
/static, /public, /resource, META-INF/resourcessrc/main/resources๋ ๋ฆฌ์๋ฅผ ๋ณด๊ดํ๋ ๊ณณ์ด๊ณ , ๋ ํด๋์คํจ์ค์ ์์ ๊ฒฝ๋ก์ด๋ค. ๋ฐ๋ผ์ ๋ค์ ๋๋ ํ ๋ฆฌ์ ๋ฆฌ์๋ฅผ ๋ฃ์ด๋๋ฉด ์คํ๋ง ๋ถํธ๊ฐ ์ ์ ๋ฆฌ์์ค๋ก ์๋น์ค๋ฅผ ์ ๊ณตํ๋ค.src/main/resources/static ์ด๋ค.src/main/resources/static/basic/hello-form.htmlhttp://localhost:8000/basic/hello-form.htmlsrc/main/resources/templates @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";
}
@RequestMapping("/response/hello")
public void responseViewV3(Model model) {
model.addAttribute("data", "hello!!");
}
}
@ResponseBody๊ฐ ์์ผ๋ฉด response/hello๋ก ๋ทฐ ๋ฆฌ์กธ๋ฒ๊ฐ ์คํ๋์ด์ ๋ทฐ๋ฅผ ์ฐพ๊ณ , ๋ ๋๋ง ํ๋ค.@ResponseBody๊ฐ ์์ผ๋ฉด ๋ทฐ ๋ฆฌ์กธ๋ฒ๋ฅผ ์คํํ์ง ์๊ณ , HTTP ๋ฉ์์ง ๋ฐ๋์ ์ง์ response/hello๋ผ๋ ๋ฌธ์๊ฐ ์
๋ ฅ๋๋ค.response/hello๋ฅผ ๋ฐํธ๋ํ๋ฉด ๋ค์ ๊ฒฝ๋ก์ ๋ทฐ ํ
ํ๋ฆฟ์ด ๋ ๋๋ง ๋๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.templates/response/hello.html@Controller๋ฅผ ์ฌ์ฉํ๊ณ , HttpServletResponse, OutputStream(Writer)๊ฐ์ HTTP ๋ฉ์์ง ๋ฐ๋๋ฅผ ์ฒ๋ฆฌํ๋ ํ๋ฆฌ๋ฏธํฐ๊ฐ ์์ผ๋ฉด ์์ฒญ URL์ ์ฐธ๊ณ ํด์ ๋
ผ๋ฆฌ ๋ทฐ ์ด๋ฆ์ผ๋ก ์ฌ์ฉ/response/hellotemplates/response/hello.html@ResponseBody, HttpEntity๋ฅผ ์ฌ์ฉํ๋ฉด, ๋ทฐ ํ
ํ๋ฆฟ์ ์ฌ์ฉํ๋ ๊ฒ์ด ์๋๋ผ, HTTP ๋ฉ์์ง ๋ฐ๋์ ์ง์ ์๋ต ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํ ์ ์๋ค.์ฐธ๊ณ
- HTML์ด๋ ๋ทฐ ํ ํ๋ฆฟ์ ์ฌ์ฉํด๋ HTTP ์๋ต ๋ฉ์์ง ๋ฐ๋์ HTML ๋ฐ์ดํฐ๊ฐ ๋ด๊ฒจ์ ์ ๋ฌ๋๋ค. ์ฌ๊ธฐ์ ์ค๋ช ํ๋ ๋ด์ฉ์ ์ ์ ๋ฆฌ์์ค๋ ๋ทฐ ํ ํ๋ฆฟ์ ๊ฑฐ์น์ง ์๊ณ , ์ง์ HTTP ์๋ต ๋ฉ์์ง๋ฅผ ์ ๋ฌํ๋ ๊ฒฝ์ฐ์ด๋ค.
@Controller ๋์ ์ @ResController ์ ๋
ธํ
์ด์
์ ์ฌ์ฉํ๋ฉด, ํด๋น ์ปจํธ๋กค๋ฌ์ ๋ชจ๋ @ResponseBody๊ฐ ์ ์ฉ๋๋ ํจ๊ณผ๊ฐ ์๋ค. ๋ฐ๋ผ์ ๋ทฐ ํ
ํ๋ฆฟ์ ์ฌ์ฉํ๋ ๊ฒ์ด ์๋๋ผ, HTTP ๋ฉ์์ง ๋ฐ๋์ ์ง์ ๋ฐ์ดํฐ๋ฅผ ์
๋ ฅํ๋ค. ์ด๋ฆ ๊ทธ๋๋ก Rest API(HTTP API)๋ฅผ ๋ง๋ค ๋ ์ฌ์ฉํ๋ ์ปจํธ๋กค๋ฌ์ด๋ค.@ResponseBody๋ ํด๋์ค ๋ ๋ฒจ์ ๋๋ฉด ์ ์ฒด ๋ฉ์๋์ ์ ์ฉ๋๋ค.@RestController ์ ๋
ธํ
์ด์
์์ @ResponseBody๊ฐ ์ ์ฉ๋์ด ์๋ค.