HTTP Message Converter
응답의 경우 클라이언트 HTTP Aceept 헤더와 서버의 Controller 반환 타입 정보를 조합해서 HTTPMessageConverter가 선택된다.
HTTP 요청 : @RequestBody, HttpEntity(RequestEntity)
HTTP 응답 : @ResponseBody, HttpEntity(ResponseEntity)
HTTP Message Converter 인터페이스
- 해당 클래스, 미디어 타입을 지원하는지 체크
- Message Converter를 통해서 메시지를 읽고 쓰는 기능
스프링 부트의 주요 Message Converter
ByteArrayHttpMessageConverter : byte[] 데이터를 처리
- 클래스 타입 : byte[]
- 미디어 타입 : */*
- request ex) @RequestBody byte[] data
- response ex) @ResponseBody return byte[] , 미디어타입 : application/octet-stream
StringHttpMessageConverter : String 문자로 데이터 처리
- 클래스 타입 : String
- 미디어 타입 : */*
- request ex) @RequestBody String data
- response ex) @ResponseBody return "ok", 미디어 타입: text/plain
MappingJackson2HttpMessageConverter : application/json
- 클래스 타입: 객체, HashMap
- 미디어 타입 : application/json 관련
- request ex) @RequestBody HelloData data
- response ex) @ResponseBody return helloData, 미디어 타입: application/json관련
HTTP 요청 데이터 읽기
- HTTP 요청이 오고, 컨트롤러에서 @RequestBody, HttpEntity 파라미터를 사용
- Message Converter가 canRead를 호출하여 클래스 타입과, 미디어 타입 확인
- canRead() 조건을 만족하면 read()를 호출하여 객체 생성, 반환
HTTP 응답 데이터 생성
- 컨트롤러에서 @ReponseBody, HttpEntity로 값이 반환
- Message Converter가 canWrite를 호출하여 return 대상의 클래스 타입과, 미디어 타입(Accept) 확인
- canWrite() 조건을 만족하면 write()를 호출하여 HTTP 응답 메시지 바디에 데이터를 생성
요청 매핑 핸들러 어댑터
동작 방식
ArgumentResolver(HandlerMethodArgumentResolver)
애노테이션 기반의 컨트롤러는 매우 다양한 파라미터를 사용할 수 있다.
HttpServletRequest, Model, @RequestParam, @ModelAttribute, @RequestBody, HttpEntity 등등..
유연하게 파라미터를 처리할 수 있게 해주는 것이 바로 ArgumentResolver이다.
애노테이션 기반 컨트롤러를 처리하는 RequestMappingHandlerAdaptor는 ArgumentResolver를 호출해서 컨트롤러(핸들러)가 필요로 하는 다양한 파라미터의 값을 생성하고 준비된 파라미터들은 컨트롤러에게 넘겨준다. (스프링은 30개가 넘는 ArgumentResolver를 기본으로 제공)
- supportsParameter()를 호출해서 해당 파라미터를 지원하는지 체크
- 치원하면 resolverArgument()를 호출하여 실제 객체를 생성
- 생성된 객체가 컨트롤러 호출시 넘어감
- 이 인터페이스를 확장하여 원하는 ArgumentResolver를 만들 수도 있음
docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann-arguments
Web on Servlet Stack
Spring Web MVC is the original web framework built on the Servlet API and has been included in the Spring Framework from the very beginning. The formal name, “Spring Web MVC,” comes from the name of its source module ({spring-framework-main-code}/sprin
docs.spring.io
ReturnValueHandler(HandlerMethodReturnValueHandler)
컨트롤러의 반환 값을 변환하고 처리한다.
컨트롤러에서 String으로 뷰 이름만 반환해도, 동작하는 이유가 이것 덕분이다.
(스프링은 10여개가 넘는 ReturnValueHandler를 지원)
docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann-return-types
Web on Servlet Stack
Spring Web MVC is the original web framework built on the Servlet API and has been included in the Spring Framework from the very beginning. The formal name, “Spring Web MVC,” comes from the name of its source module ({spring-framework-main-code}/sprin
docs.spring.io
스프링 MVC는 @RequestBody, @ResponseBody가 있으면 RequestResponseBodyMethodProcessor
HttpEntity가 있으면 HttpEntityMethodProcessor를 사용한다.
각각의 ArgumentResolver들이 Http Message Converter를 사용해서 필요한 객체를 생성
마찬가지로 각각의 해당하는 ReturnValueHandler들이 Http Message Converter를 사용하여 응답 결과를 만듬