Spring API (@RequestBody와 ResponseEntity의 차이점)

Back-End/API 2019. 6. 27. 11:43
728x90
반응형

Spring API (@RequestBody와 ResponseEntity의 차이점)


 

Spring에서는 HttpEntity란 클래스를 제공하는데 이 클래스의 역할은 Http 프로토콜을 이용하는 통신의 header과


body 관련 정보를 저장할 수 있게끔 합니다. 그리고 이를 상속받은 클래스로 RequestEntity와 ResponseEntity가

존재한다.

즉, 통신 메시지 관련 header과 body의 값들을 하나의 객체로 저장하는 것이 HttpEntity 클래스 객체이고,

Request 부분일 경우 HttpEntity를 상속받은 RequestEntity가, Response 부분일 경우 HttpEntity를 상속받은

ResponseEntity가 하게 됩니다.

@ResponseBody나 ResponseEntity를 return 하는거나 결과적으로는 같은 기능이지만.. 그 구현 방법이 틀리다.

예를 들어 header 값을 변경시켜야 할 경우엔 @ResponseBody의 경우 파라미터로 Response 객체를 받아서

이 객체에서 header를 변경시켜야 하고,.. ResponseEntity에서는 이 클래스 객체를 생성한뒤 객체에서

header 값을 변경시키면 된다.

자세한것은 Spring API 문서를 참고하면 된다.




ResponseEntity API


1
2
3
4
5
6
7
8
9
public class ResponseEntity<T> extends HttpEntity<T> {
 
    private final Object status;
 
 
    /**
     * Create a new {@code ResponseEntity} with the given status code, and no body nor headers.
     * @param status the status code
     */
cs




@RequestBody API


1
2
3
4
5
6
7
8
9
10
11
12
13
public @interface RequestBody {
 
    /**
     * Whether body content is required.
     * <p>Default is {@code true}, leading to an exception thrown in case
     * there is no body content. Switch this to {@code falseif you prefer
     * {@code null} to be passed when the body content is {@code null}.
     * @since 3.2
     */
    boolean required() default true;
 
}
cs


728x90
반응형

'Back-End > API' 카테고리의 다른 글

Spring API (@RequestMapping이 사용하는 속성)  (0) 2019.06.30
Spring API ( HttpServletRequest, HttpServletResponse )  (0) 2019.06.27
스프링 AOP JoinPoint 객체  (0) 2019.06.23
서블릿 API  (0) 2019.04.30
java.io 패키지  (0) 2019.04.21
: