🙈

⃝ 동글동글 ⃝

🪐ᐩ˖ 🍎

Spring/Spring Boot

[Spring Boot] ResponseEntity란? (HttpHeaders, HttpBody)

JONG_UK 2023. 3. 30. 19:42
728x90
반응형
SMALL

Request(요청), Response(응답)

프론트와 백엔드가 HTTP 통신을 하며 데이터를 전달할 때 대부분 JSON 형식으로 전달을 하게 된다.

 

주로 응답(response)의 내용으로는 Http Header, Http Body(status code, message, data) 가 있다.  

Http Header에는 ContentType, Authorization 등 필요한 정보가 포함되어 있어야 하고, 

Http Body는 프론트나 서버에서 서로 전송할 데이터의 내용(값)들을 포함하게 된다.

 

Http Header와 Http Body를 하나씩 만들어 전송을 해도 되지만, 백엔드에서 요청에 대한 응답으로 조금 더 쉽고 간편하게 할 수 있는 방법이 ResponseEntity를 사용하는 것이다.

 

지금부터 ResponseEntity란 무엇인지, 그리고 Http Header와 Http Body를 만드는 방법에 대해 예시를 보면서 해보자.

 

ResponseEntity란?

ResponseEntity란 Spring Framework에서 제공하는 클래스이다. 

 

Spring Framework에서 제공하는 클래스 중 HttpEntity라는 클래스가 있는데, HttpEntity 클래스는 HttpHeader와 HttpBody를 포함하고 있는 클래스이다. 

 

ResponseEntity 클래스는 위와 같은 특성을 가진 HttpEntity를 상속받아 구현된 클래스이다. 

 

ResponseEntity와 동일하게 RequestEntity도 있는데 이건 추후에 설명하도록 하겠습니다.

 

Spring Framework는 ResponseEntity에 대해 아래와 같이 정의하고 있다.

public class ResponseEntity<T> extends HttpEntity<T> {

   private final Object status;


   /**
    * Create a {@code ResponseEntity} with a status code only.
    * @param status the status code
    */
   public ResponseEntity(HttpStatus status) {
      this(null, null, status);
   }

   /**
    * Create a {@code ResponseEntity} with a body and status code.
    * @param body the entity body
    * @param status the status code
    */
   public ResponseEntity(@Nullable T body, HttpStatus status) {
      this(body, null, status);
   }

   /**
    * Create a {@code ResponseEntity} with headers and a status code.
    * @param headers the entity headers
    * @param status the status code
    */
   public ResponseEntity(MultiValueMap<String, String> headers, HttpStatus status) {
      this(null, headers, status);
   }

   /**
    * Create a {@code ResponseEntity} with a body, headers, and a status code.
    * @param body the entity body
    * @param headers the entity headers
    * @param status the status code
    */
   public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, HttpStatus status) {
      this(body, headers, (Object) status);
   }

   /**
    * Create a {@code ResponseEntity} with a body, headers, and a raw status code.
    * @param body the entity body
    * @param headers the entity headers
    * @param rawStatus the status code value
    * @since 5.3.2
    */
   public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, int rawStatus) {
      this(body, headers, (Object) rawStatus);
   }

   /**
    * Private constructor.
    */
   private ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, Object status) {
      super(body, headers);
      Assert.notNull(status, "HttpStatus must not be null");
      this.status = status;
   }

 

하지만 이것만 보면 솔직히 이해할 수 없다.

 

간단하게 프론트에서 name을 파라미터로 담아 서버에 보내서 응답을 받아오는 요청을 해 보도록 하겠습니다.

 

DTO 클래스

@Data
public class ResponseDto {
    private HttpStatus status;
    private String name;
    private String content;
    private Object data;
}

 

Controller 클래스

@RestController
@RequiredArgsConstructor
public class ResponseEntityController {
    @GetMapping("/response")
    public ResponseEntity<ResponseDto> responseEntityTest(HttpServletRequest request) {
        ResponseDto responseDto = new ResponseDto();

        String name = request.getParameter("name");
        responseDto.setStatus(HttpStatus.OK);
        responseDto.setName(name);
        responseDto.setContent("응답 성공 테스트 입니다.");

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));

        return new ResponseEntity<>(responseDto, headers, HttpStatus.OK);
		//return ResponseEntity.ok().headers(headers).body(responseDto);
    }
}

 

get 요청을 했을 때 name 값을 가져와 Dto 클래스에 값을 넣어주는 코드입니다.

 

Postman으로 확인해보면 응답이 올바르게 들어간 것을 볼 수 있습니다.

 

테스트 Controller의 마지막 줄에 return ResponseEntity.ok().headers(headers).body(responseDto); 가 있는데

이것은 ResponseEntity.ok()라면 200 상태코드,

ResponseEntity.badRequest()라면 출력 값은 같을지 몰라도 상태코드가 400으로 나타납니다.

 

따라서 프론트의 요청에 대해 백엔드에서 try catch문을 통해 작업을 하거나 조건문을 통해 상태코드 200을 보낼지 400을 보낼지 또는 401, 500을 보낼지는 코딩하기에 따라 다릅니다. 

728x90
반응형
LIST