REST 관련 개념.
RestTemplate
postForEntity: POST 요청을 보내고 결과로 ResponseEntity로 반환받는다.
postForEntity() 메서드는 ResponseEntity<T> 객체로 데이터를 받을 수 있습니다.
//when
ResponseEntity<Long> responseEntity=restTemplate.
postForEntity(url,requestDto,Long.class);
exchange: HTTP 헤더를 새로 만들 수 있고 어떤 HTTP 메서드도 사용가능하다.
HttpEntity<PostsUpdateRequestDto> requestEntity=new HttpEntity<>(requestDto);
//when
ResponseEntity<Long> responseEntity=restTemplate.
exchange(url, HttpMethod.PUT,
requestEntity,Long.class);
ResponseEntity란?
Spring Framework에서 제공하는 클래스 중 HttpEntity라는 클래스가 존재한다. 이것은 HTTP 요청(Request) 또는 응답(Response)에 해당하는 HttpHeader와 HttpBody를 포함하는 클래스이다.
public class HttpEntity<T> {
private final HttpHeaders headers;
@Nullable
private final T body;
}
public class RequestEntity<T> extends HttpEntity<T>
public class ResponseEntity<T> extends HttpEntity<T>
HttpEntity 클래스를 상속받아 구현한 클래스가 RequestEntity, ResponseEntity 클래스이다. 따라서 ResponseEntity는 사용자의 HttpRequest에 대한 응답 데이터를 포함하는 클래스이다. 따라서 HttpStatus, HttpHeaders, HttpBody를 포함한다.
참조:devlog-wjdrbs96.tistory.com/182