본문 바로가기

개발/Spring

[Spring] ResponseEntity / RequestEntity

ResponseEntity / RequestEntity

ResponseEntity와 RequestEntity는 스프링에서 제공하는 클래스이다.

HttpEntity를 상속받는 RequestEntity 클래스

 

두 클래스 모두 스프링에서 제공하는 HttpEntity 클래스를 상속받아서 구현된다. HttpEntity는 Httpheader와 Httpbody를 포함하고 있는 클래스이다. 따라서 그것을 상속 받는 클래스인 ResponseEntity와 RequestEntity는 Httpheader와 Httpbody를 포함하고 있다.

 

보통 ResponseEntity는 Controller나 ExceptionHandler에서 클라이언트로 응답 데이터를 보내줄 때 사용한다. 반대로 클라이언트에서 보낸 정보는 RequestEntity로 받아서 Controller 등에서 정보를 확인 할 수 있다.

 

 

 

 

 

RequestEntity

RequestEntity 클래스

RequestEntity 클래스를 보면 HttpEntity에서 상속받는 HttpHeader, Httpbody 외에도 HttpMethod, URI, Type 필드를 가지고 있다. 필드 값 중 원하는 값만 넣어주는 생성자를 사용하여 RequestEntity를 사용할 수 있다.

 

 

 

 

ResponseEntity

ResponseEntity

ResponseEntity 클래스를 보면 HttpEntity에서 상속받는 HttpHeader, Httpbody 외에도 HttpStatuscode를 필드로 가지고 있다. 필드 값 중 원하는 값만 넣어주는 생성자를 사용하여 ResponseEntity를 객체로 사용할 수 있다.