티스토리 뷰

 

요약

1. @RequestBody는 HttpMessageConverter HTTP Request Body 내의 데이터를 객체로 변환(역직렬화)하도록 시키는 애노테이션이다.

2. @Valid를 붙이면 검증을 할 수 있고, 실패 시 MethodArgumentNotValidException을 던진다.

3. application/json 타입의 경우 MappingJackson2HttpMessageConverter가 데이터를 객체로 역직렬화 해준다.

4. MappingJackson2HttpMessageConverter는 ObjectMapper를 사용하는데, 프로퍼티를 이용하기 때문에 getter / setter 둘 중 하나만 있으면 된다

 

@RequestBody

- Annotation indicating a method parameter should be bound to the body of the web request.

(메서드 파라미터가 web request(HTTP Request)의 body 에 있는 데이터를 통해 bind 될 것을 나타내는 애노테이션)

(굉장히 정직한 이름이다;;)

 

- The body of the request is passed through an HttpMessageConverter to resolve the method argument depending on the content type of the request.

(http body는 HttpMessageConverter에게 전달되어 처리된다.)

 

- Optionally, automatic validation can be applied by annotating the argument with @Valid.

(@Valid 애노테이션과 함께 사용하면 자동으로 검증이 적용된다)

 

@PostMapping("/accounts")
public void handle(@RequestBody Account account) {
    // ...
}

 

- You can use the @RequestBody annotation to have the request body read and deserialized into an Object through an HttpMessageConverter.

(request Body를 읽고 역직렬화를 통해 데이터를 객체로 만드는 과정HttpMessageConverter가 해준다)

 

- By default, validation errors cause a MethodArgumentNotValidException, which is turned into a 400 (BAD_REQUEST) response.

(@Valid를 사용할 경우, 검증 실패 시 MethodArgumentNotValidException을 발생시킨다.)

 

- Alternatively, you can handle validation errors locally within the controller through an Errors or BindingResult argument

(예외를 바로 던지지 않고 컨트롤러에서 처리하고 싶다면 BindingResult를 뒤에 바로 붙여주면 된다)

@PostMapping("/accounts")
public void handle(@Valid @RequestBody Account account, BindingResult result) {
    // ...
}

 

# HttpMessageConverter

- The spring-web module contains the HttpMessageConverter contract for reading and writing the body of HTTP requests and responses through InputStream and OutputStream. 

(spring-web 모듈은 HttpMessageConverter를 기본적으로 포함하고 있다.

-> InputStream에서 HTTP request의 body를 읽는 역할 & OutputStream에서 HTTP response의 body에 데이터를 쓰는 역할을 담당한다)

 

- HttpMessageConverter instances are used on the client side (for example, in the RestTemplate) and on the server side (for example, in Spring MVC REST controllers).

(클라이언트 사이드에서는 RestTemplate, 서버 사이드에서는 RestController 구현 할 때 주로 사용된다.)

 

- Concrete implementations for the main media (MIME) types are provided in the framework and are, by default, registered with the RestTemplate on the client side and with RequestMappingHandlerAdapter on the server side (see Configuring Message Converters).

(HttpMessageConverter(인터페이스)의 구현체는 MIME 타입에 의해 정해진다

-> RequestMappingHandlerAdapter가 이를 판단하고 적절한 HttpMessageConverter를 사용할 수 있게 해준다)

 

 

<MIME 3대장>

text/* -> StringHttpMessageConverter

application/x-www-form-urlencoded -> FormHttpMessageConverter

application/json -> MappingJackson2HttpMessageConverter

 

 

# MappingJackson2HttpMessageConverter

- Implementation of org.springframework.http.converter.HttpMessageConverter that can read and write JSON using Jackson 2.x's  ObjectMapper.

(Jackson 라이브러리의 ObjectMapper를 사용해서 JSON을 읽거나 쓰도록 만들어주는 HttpMessageConverter 구현체)

 

- This converter can be used to bind to typed beans, or untyped HashMap instances.

(typed beans(??) 또는 untyped HashMap 인스턴스를 바인딩 하는데 사용)

 

 

<결론>

메서드 파라미터에 @RequestBody가 붙어있다

-> HTTP Request Body 내부에 데이터가 담겨있음을 의미

-> RequestMappingHandlerAdapter가 데이터의 MIME 타입을 확인하고 적절한 HttpMessageConverter 구현체 선정

    (application/json의 경우 MappingJackson2HttpMessageConverter 사용)

-> 해당 Converter가 Request Body 내부의 데이터를 객체(Object)로 변환!

    (MappingJackson2HttpMessageConverter의 경우 내부적으로 ObjectMapper를 이용해 변환)

 

* RequestBody의 DTO는 setter 없이 getter만 있으면 getter를 이용해 값을 바인딩할 수 있다

** 어떻게 가능할까? 후속 포스팅 ==> https://sedangdang.tistory.com/307


+ HTTP Request 구조

* Start Line (시작 줄)

- HTTP 메서드 (GET, PUT, POST, DELETE, HEAD, OPTIONS 등....)

- 요청 타깃 (주로 URL, 프로토콜, 포트, 도메인의 절대 경로)

- HTTP Version

 

* Body (본문)

- 필수 요소 X (GET, HEAD, DELETE, OPTIONS와 같이 리소스를 가져오는 요청은 보통 본문이 필요하지 않다)

- POST와 같이 서버에 데이터를 전송하고자 할 때 주로 사용

- 단일 리소스 바디 (Content-Type, Content-Length로 정의)

- 다중 리소스 바디 (Multipart form 데이터)

(* 참고 ) https://developer.mozilla.org/ko/docs/Web/HTTP/Messages#http_%EC%9A%94%EC%B2%AD


 

진짜로 파면 팔수록 더 어렵다;;

 

 

<참고>

 

RequestBody (Spring Framework 6.0.2 API)

Whether body content is required. Default is true, leading to an exception thrown in case there is no body content. Switch this to false if you prefer null to be passed when the body content is null.

docs.spring.io

 

 

Web on Servlet Stack

This part of the reference documentation covers support for Servlet stack, WebSocket messaging that includes raw WebSocket interactions, WebSocket emulation through SockJS, and publish-subscribe messaging through STOMP as a sub-protocol over WebSocket. = I

docs.spring.io

 

 

 

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함