implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.2.2'
- controller - RestTestController 추가
package org.board.controller;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/rest/")
@Log4j2
public class RestTestController {
@GetMapping("/test")
public String[] RestTest(){
return new String[]{"AAA","BBB","CCC","DDD"};
}
}
- localhost:8080/rest/test 호출
- domain 패키지 - TodoVO 클래스 생성
package org.board.domain;
import lombok.Builder;
import lombok.ToString;
import java.time.LocalDate;
@ToString @Builder
public class TodoVO {
private Long bno;
private String title;
private LocalDate dueDate;
private boolean finished;
}
@GetMapping("/test/{bno}")
public String[] RestTest2(@PathVariable("bno") Integer bno, @RequestParam Map<String,Object> map){
log.info("------------------------------------------");
log.info(bno);
log.info(map.get("name"));
log.info(map.get("age"));
return new String[]{(String)map.get("name"), (String)map.get("age")};
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Hello</title>
</head>
<body>
<h1>Hello Controller</h1>
<h1>Hello Controller</h1>
<button class="testBtn" data-bno="4">Ajax Test</button>
<ul class="Ul">
</ul>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
document.querySelector(".testBtn").addEventListener("click", (e)=>{
e.stopPropagation()
e.preventDefault()
const bno = parseInt(e.target.getAttribute("data-bno"))
ajaxTest()
},false)
async function ajaxTest(){
const obj={
name:"choigd",
age:"19",
}
const res = await axios.get('/rest/test/33',{params: obj})
const list = res.data
let str=""
for(let i=0; i< list.length; i++){
str +=`<li>\${list[i]}</li>`
}
console.log(str)
document.querySelector(".Ul").innerHTML = str
}
</script>
</body>
</html>