<aside> 📖 스프링 MVC의 기본 기능
</aside>
@RequestMapping , HTTP 메서드 매핑 어노테이션, PathVariable , 조건 매핑request.getParameter() , @RequestParam (생략 가능), @ModelAttribute (생략 가능) 등InputStream , HttpEntity , @RequestBody (생략 불가, 메시지 컨버터)response.getWriter().write() , ResponseEntity , @ResponseBody@RequestBody , HttpEntity(RequestEntity)@ResponseBody, HttpEntity(ResponseEntity)@RequestBody , HttpEntity(RequestEntity) 의 경우 호출@ResponseBody, HttpEntity(ResponseEntity) 의 경우 호출/resources/static/ 위치에 index.html 파일을 두면 웰컴 페이지로 처리해요청 인입 시 어떤 URL 의 컨트롤러가 매핑되어야 하는지
요청 매핑 테스트
@RestController
public class MappingController {
private Logger log = LoggerFactory.getLogger(getClass());
@RequestMapping("/hello-basic")
public String helloBasic() {
log.info("helloBasic");
return "ok";
}
}
@RestController : 반환 값으로 뷰를 찾는 게 아니라 HTTP 메시지 바디에 바로 입력 → 실행 결과로 ok 메시지 받을 수 있음 (@ResponseBody 와 관련 있음)
@RequestMapping("/hello-basic") : 해당 URL 로 요청이 오면 이 메서드가 호출되도록 매핑
{”hello-basic”, “/hello-go”})/hello-basic 와 /hello-basic/ 은 서로 다른 URL 이지만 스프링은 둘 다 같은 요청으로 간주하고 /hello-basic 로 매핑함
HTTP 메서드 : @RequestMapping 에 method 속성으로 HTTP 메서드를 지정하지 않으면 HTTP 메서드와 무관하게 호출 (모두 허용)
HTTP 메서드 매핑 테스트
@RestController
public class MappingController {
private Logger log = LoggerFactory.getLogger(getClass());
@RequestMapping(value="/hello-basic", method=RequestMethod.GET)
public String helloBasic() {
log.info("helloBasic");
return "ok";
}
}
HTTP 메서드 매핑 축약
@GetMapping(value="/hello-basic")
public String helloBasic() {
log.info("helloBasic");
return "ok";
}
@RequestMapping 과 method 를 지정해서 사용하는 것 확인 가능경로 변수 (PathVariable)
@GetMapping("/mapping/{userId}")
public String mappingPath(@PathVariable("userId") String data) {
log.info("mappingPath userID={}", data);
return "ok";
}
@PathVariable : 최근 HTTP API 에서 리소스 경로에 식별자를 넣을 경우 @PathVariable 을 통해 매핑되는 부분을 편리하게 조회 가능
@PathVariable 의 이름과 파라미터의 이름이 같을 경우 이름 생략 가능
(@PathVariable String userId)