[์ฐธ๊ณ ๊ฐ์] ๊น์ํ๋์ ์คํ๋ง MVC 1ํธ - ๋ฐฑ์๋ ์น ๊ฐ๋ฐ ํต์ฌ ๊ธฐ์
์๋ฒ์์ ์๋ต ๋ฐ์ดํฐ๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ ํฌ๊ฒ 3๊ฐ์ง๋ค
์คํ๋ง ๋ถํธ๋ ํด๋์คํจ์ค์ ๋ค์ ๋๋ ํ ๋ฆฌ์ ์๋ ์ ์ ๋ฆฌ์์ค๋ฅผ ์ ๊ณตํ๋ค.
/static, /public, /resources, /META-INF/resources
src/main/resources๋ ๋ฆฌ์์ค๋ฅผ ๋ณด๊ดํ๋ ๊ณณ์ด๊ณ , ๋ ํด๋์คํจ์ค์ ์์ ๊ฒฝ๋ก์ด๋ค. ๋ฐ๋ผ์ ๋ค์ ๋๋ ํ ๋ฆฌ์ ๋ฆฌ์์ค๋ฅผ ๋ฃ์ด๋๋ฉด ์คํ๋ง ๋ถํธ๊ฐ ์ ์ ๋ฆฌ์์ค๋ก ์๋น์ค๋ฅผ ์ ๊ณตํ๋ค.
src/main/resources/static
๋ค์ ๊ฒฝ๋ก์ ํ์ผ์ด ๋ค์ด์์ผ๋ฉด
src/main/resources/static/basic/hello-form.html
์น ๋ธ๋ผ์ฐ์ ์์ ๋ค์๊ณผ ๊ฐ์ด ์คํํ๋ฉด ๋๋ค.
http://localhost:8080/basic/hello-form.html
์ ์ ๋ฆฌ์์ค๋ ํด๋น ํ์ผ์ ๋ณ๊ฒฝ ์์ด ๊ทธ๋๋ก ์๋น์คํ๋ ๊ฒ์ด๋ค.
๋ทฐ ํ
ํ๋ฆฟ์ ๊ฑฐ์ณ์ HTML์ด ์์ฑ๋๊ณ , ๋ทฐ๊ฐ ์๋ต์ ๋ง๋ค์ด์ ์ ๋ฌํ๋ค.
์คํ๋ง ๋ถํธ๋ ๊ธฐ๋ณธ ๋ทฐ ํ
ํ๋ฆฟ ๊ฒฝ๋ก๋ฅผ ์ ๊ณตํ๋ค.
๋ทฐ ํ
ํ๋ฆฟ ๊ฒฝ๋ก
src/main/resources/templates
๋ทฐ ํ
ํ๋ฆฟ ์์ฑ
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="${data}">empty</p>
</body>
</html>
๋ทฐ ํ ํ๋ฆฟ์ ํธ์ถํ๋ ์ปจํธ๋กค๋ฌ
@Controller
public class ResponseViewController {
@RequestMapping("/response-view-v1")
public ModelAndView responseViewV1() {
ModelAndView mav = new ModelAndView("response/hello")
.addObject("data", "hello!");
return mav;
}
@RequestMapping("/response-view-v2")
public String responseViewV2(Model model) {
model.addAttribute("data","hello!");
return "response/hello";
}
//๊ถ์ฅ X
@RequestMapping("/response/hello")
public void responseViewV3(Model model) {
model.addAttribute("data","hello!");
}
}
@ResponseBody๊ฐ ์์ผ๋ฉด response/hello๋ก ๋ทฐ ๋ฆฌ์กธ๋ฒ๊ฐ ์คํ๋์ด์ ๋ทฐ๋ฅผ ์ฐพ๊ณ , ๋ ๋๋งํ๋ค.
@ResponseBody๊ฐ ์์ผ๋ฉด ๋ทฐ ๋ฆฌ์กธ๋ฒ๋ฅผ ์คํํ์ง ์๊ณ , HTTP ๋ฉ์์ง ๋ฐ๋์ ์ง์ response/hello๋ผ๋ ๋ฌธ์๊ฐ ์
๋ ฅ๋๋ค.
๋ทฐ ๋
ผ๋ฆฌ ์ด๋ฆ์ธ response.hello
๋ฅผ ๋ฐํํ๋ฉด ๋ค์ ๊ฒฝ๋ก์ ๋ทฐ ํ
ํ๋ฆฟ์ด ๋ ๋๋ง๋๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
@ResponseBody, HttpEntity๋ฅผ ์ฌ์ฉํ๋ฉด, ๋ทฐ ํ ํ๋ฆฟ์ ์ฌ์ฉํ๋ ๊ฒ์ด ์๋๋ผ, HTTP๋ฉ์์ง ๋ฐ๋์ ์ง์ ์๋ต ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํ ์ ์๋ค.
HTTP API๋ฅผ ์ ๊ณตํ๋ ๊ฒฝ์ฐ HTTP ๋ฉ์์ง ๋ฐ๋์ JSON ๊ฐ์ ํ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ค์ด ๋ณด๋ธ๋ค.
@Slf4j
@Controller
@ResponseBody
// @RestController
public class ResponseBodyController {
@GetMapping("/response-body-string-v1")
public void responseBodyV1(HttpServletResponse response) throws IOException {
response.getWriter().write("ok");
}
@GetMapping("/response-body-string-v2")
public ResponseEntity<String> responseBodyV2(HttpServletResponse response) throws IOException {
return new ResponseEntity<>("ok", HttpStatus.OK);
}
// @ResponseBody
@GetMapping("/response-body-string-v3")
public String responseBodyV3() {
return "ok";
}
@GetMapping("/response-body-json-v1")
public ResponseEntity<HelloData> responseBodyJsonV1() {
HelloData helloData = new HelloData();
helloData.setUsername("userA");
helloData.setAge(20);
return new ResponseEntity<>(helloData, HttpStatus.OK);
}
@ResponseStatus(HttpStatus.OK)
// @ResponseBody
@GetMapping("/response-body-json-v2")
public HelloData responseBodyJsonV2() {
HelloData helloData = new HelloData();
helloData.setUsername("userA");
helloData.setAge(20);
return helloData;
}
}
responseBodyV1
์๋ธ๋ฆฟ์ ์ง์ ๋ค๋ฃฐ ๋ ์ฒ๋ผ HttpServletResponse ๊ฐ์ฒด๋ฅผ ํตํด์ HTTP ๋ฉ์์ง ๋ฐ๋์ ์ง์ ok ์๋ต ๋ฉ์์ง๋ฅผ ์ ๋ฌํ๋ค.
response.getWriter().write("ok")
responseBodyV2
responseEntity ์ํฐํฐ๋ HttpEntity๋ฅผ ์์ ๋ฐ์๋๋ฐ, HttpEntity๋ HTTP ๋ฉ์์ง์ ํค๋, ๋ฐ๋ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์๋ค. ResponseEntity๋ ์ฌ๊ธฐ์ ๋ํด์ HTTP ์๋ต ์ฝ๋๋ฅผ ์ค์ ํ ์ ์๋ค.
responseBodyV3
@ResponseBody๋ฅผ ์ฌ์ฉํ๋ฉด view๋ฅผ ์ฌ์ฉํ์ง ์๊ณ , HTTP ๋ฉ์์ง ์ปจ๋ฒํฐ๋ฅผ ํตํด์ HTTP ๋ฉ์์ง๋ฅผ ์ง์ ์
๋ ฅํ ์ ์๋ค. ResponseEntity๋ ๋์ผํ ๋ฐฉ์์ผ๋ก ๋์.
responseBodyJsonV1
ResponseEntity๋ฅผ ๋ฐํํ๋ค. HTTP ๋ฉ์์ง ์ปจ๋ฒํฐ๋ฅผ ํตํด์ JSON ํ์์ผ๋ก ๋ณํ๋์ด์ ๋ฐํ๋๋ค.
responseBodyJsonV2
ResponseEntity๋ HTTP ์๋ต ์ฝ๋๋ฅผ ์ค์ ํ ์ ์๋๋ฐ, @ResponseBody๋ฅผ ์ฌ์ฉํ๋ฉด ์ด๋ฐ ๊ฒ์ ์ค์ ํ๊ธฐ ๊น๋ค๋กญ๋ค.
@ResponseStatus(HttpStatus.OK) ์ ๋
ธํ
์ด์
์ ์ฌ์ฉํ๋ฉด ์๋ต ์ฝ๋๋ ์ค์ ํ ์ ์๋ค.
@RestController
@RestController๋ฅผ ์ฌ์ฉํ๋ฉด ํด๋น ์ปจํธ๋กค๋ฌ์ ๋ชจ๋ @ResponseBody๊ฐ ์ ์ฉ๋๋ ํจ๊ณผ๊ฐ ์๋ค. ๋ฐ๋ผ์ ๋ทฐ ํ ํ๋ฆฟ์ ์ฌ์ฉํ๋ ๊ฒ์ด ์๋๋ผ HTTP ๋ฉ์์ง ๋ฐ๋์ ์ง์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฅํ๋ค. ์ด๋ฆ ๊ทธ๋๋ก REST API๋ฅผ ๋ง๋ค ๋ ์ฌ์ฉํ๋ ์ปจํธ๋กค๋ฌ์ด๋ค.
์ฐธ๊ณ ๋ก @ResponseBody ๋ ํด๋์ค ๋ ๋ฒจ์ ๋๋ฉด ์ ์ฒด์ ๋ฉ์๋์ ์ ์ฉ๋๋๋ฐ, @RestController ์๋ ธํ ์ด์ ์์ @ResponseBody ๊ฐ ์ ์ฉ๋์ด ์๋ค.
HTTP API์ฒ๋ผ JSON ๋ฐ์ดํฐ๋ฅผ HTTP ๋ฉ์์ง ๋ฐ๋์์ ์ง์ ์ฝ๊ฑฐ๋ ์ฐ๋ ๊ฒฝ์ฐ HTTP ๋ฉ์์ง ์ปจ๋ฒํฐ๋ฅผ ์ฌ์ฉํ๋ฉด ํธ๋ฆฌํ๋ค.
์คํ๋ง MVC๋ ๋ค์์ ๊ฒฝ์ฐ์ HTTP ๋ฉ์์ง ์ปจ๋ฒํฐ๋ฅผ ์ ์ฉํ๋ค.
HTTP ๋ฉ์์ง ์ปจ๋ฒํฐ๋ HTTP ์์ฒญ, HTTP ์๋ต ๋ ๋ค ์ฌ์ฉ๋๋ค.
์คํ๋ง ๋ถํธ๋ ๋์ ํด๋์ค ํ์ ๊ณผ ๋ฏธ๋์ด ํ์ ๋์ ์ฒดํฌํด์ ์ฌ์ฉ์ฌ๋ถ๋ฅผ ๊ฒฐ์ ํ๋ค.๋ง์ฝ ๋ง์กฑํ์ง ์์ผ๋ฉด ๋ค์ ๋ฉ์์ง ์ปจ๋ฒํฐ๋ก ์ฐ์ ์์๊ฐ ๋์ด๊ฐ๋ค.
์์ฒญ ๋งคํ ํธ๋ค๋ฌ ์ด๋ํฐ ๊ตฌ์กฐ๋ ์ ๊ฐ ์ดํด๋ฅผ ์ ๋ชปํด์ ๋ ์ดํดํ๊ณ ์ ๋ฆฌํ๊ฒ ์ต๋๋น ..๐คซ