해당 포스팅에서는 HttpEntity, ResponseEntity, RequestEntity에 대해 간략하게 정리해 보고자 한다.
HttpEntity는 HTTP 요청 또는 응답 메시지의 header, body 정보를 편리하게 조회 할 수 있는 기능을 제공한다.
@PostMapping("/request-body-string")
public HttpEntity<String> requestBodyString(HttpEntity<String> httpEntity) {
String messageBody = httpEntity.getBody();
log.info("messageBody={}", messageBody);
return new HttpEntity<>("ok");
}
- HttpEntity의 상속 클래스이며, url 요청을 보낼 때 사용한다.
- header, body, method, url, type을 생성자 파라미터로 넘길 수 있다.
- HttpEntity를 상속 받아서 HTTP 메시지의 헤더, 바디 정보를 가지고 있다.
- HTTP 응답 코드를 설정할 수 있다.
- body, header, status를 생성자 파라미터로 넘길 수 있다.
//HTTP
@GetMapping("/response-body-string")
public ResponseEntity<String> responseBody() {
return new ResponseEntity<>("ok", HttpStatus.OK);
}
//JSON
@GetMapping("/response-body-json")
public ResponseEntity<HelloData> responseBodyJson() {
HelloData helloData = new HelloData();
helloData.setUsername("userA");
helloData.setAge(20);
return new ResponseEntity<>(helloData, HttpStatus.OK);
}
참조
https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-mvc-1
https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%ED%95%B5%EC%8B%AC-%EC%9B%90%EB%A6%AC-%EA%B8%B0%EB%B3%B8%ED%8E%B8
https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-mvc-2