스프링부트-기초02

Jonguk Kim·2021년 11월 20일
0

스프링부트

목록 보기
2/2

1. 정적 컨텐츠

2. MVC와 템플릿 엔진

@Controller
  public class HelloController {
      @GetMapping("hello-mvc")
      public String helloMvc(@RequestParam("name") String name, Model model) {
          model.addAttribute("name", name);
          return "hello-template";
      }
}
  • View (resources/template/hello-template.html)
<html xmlns:th="http://www.thymeleaf.org">
  <body>
  <p th:text="'hello ' + ${name}">hello! empty</p>
  </body>
 </html>  

3. API

  • @ResponseBody 문자 반환
    • @ResponseBody를 사용하면 뷰 리졸버( viewResolver )를 사용하지 않음
    • 대신에 HTTP의 BODY에 문자 내용을 직접 반환(HTML BODY TAG를 말하는 것이 아님)
  • @ResponseBody를 사용하고, 객체를 반환하면 객체가 JSON으로 변환됨
  • 실행: http://localhost:8080/hello-api?name=spring
  • controller
@Controller
  public class HelloController {
      @GetMapping("hello-string")
      @ResponseBody
      public String helloString(@RequestParam("name") String name) {
          return "hello " + name;
      }
}

  • @ResponseBody를 사용
    • HTTP의 BODY에 문자 내용을 직접 반환
    • viewResolver 대신에 HttpMessageConverter 가 동작
      (클라이언트의 HTTP Accept 해더와 서버의 컨트롤러 반환 타입 정보 둘을 조합해서 HttpMessageConverter 가 선택)
    • 기본 문자처리: StringHttpMessageConverter
    • 기본 객체처리: MappingJackson2HttpMessageConverter
    • byte 처리 등등 기타 여러 HttpMessageConverter가 기본으로 등록되어 있음
profile
개발일지

0개의 댓글