Spring Framework에서 제공하는 클래스 중 HttpEntity라는 클래스가 존재한다.
이것은 HTTP 요청(Request) 또는 응답(Response)에 해당하는 HttpHeader와 HttpBody를 포함하는 클래스다.
그러므로 ResponseEntity에서는 HTTP header, body, status를 함께 반환할 수 있다.
ResponseEntity를 사용하지 않고 그냥 객체를 반환하면 되지 않을까?
@RequestBody가 붙은 곳에서 객체를 HttpMessage로 자동 변환 해준다.
또한, JSON형태로 변환이 필요하다면 MappingJacksonHttpMessageConverter가 자바 Object와 JSON문서 사이에서 자동으로 변화나해준다.
@ResponseBody를 붙이면 알맞은 HttpMessageConter가 동작해서 header, body, status를 만들어준다.
@ResponseStatus(HttpStatus.OK)를 사용해서 직접 상태를 만들어 줄수 있다.ResponseEntity를 사용하면, 객체와 status를 함께 보내줄 수 있다. 때문에 @ResponseStatus를 사용하지 않아도 되고 메서드 별로 다른 status를 리턴할 수 있기 때문에 좀 더 세밀하게 상태 전송이 가능하다.
public class HttpEntity<T> {
private final HttpHeaders headers;
@Nullable
private final T body;
}
public class ResquestEntity<T> extends HttpEntity<T>
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;
}
}
@PostMapping("/post/like")
public ResponseEntity<SetLikeDto.Response> updateLike(@RequestBody SetLikeDto.Request postLikeDto, @AuthenticationPrincipal UserDetailsImpl userDetails){
Post post = postService.setPostLike(postLikeDto,userDetails.getUser());
SetLikeDto.Response response = modelMapper.map(post, SetLikeDto.Response.class);
return ResponseEntity.ok().build();
}
@PostMapping("/post/like")
public ResponseEntity<SetLikeDto.Response> updateLike(@RequestBody SetLikeDto.Request postLikeDto, @AuthenticationPrincipal UserDetailsImpl userDetails){
Post post = postService.setPostLike(postLikeDto,userDetails.getUser());
SetLikeDto.Response response = modelMapper.map(post, SetLikeDto.Response.class);
return ResponseEntity.ok(response);
}
제네릭 타입으로 데이터타입을 명시하지않고, 런타임까지 유연하게 가져가겠는 의미이다.
특별하게 타입을 지정하지 않고, 되는데로 다 받아준다는 의미로 생각할 수 있다.
@PostMapping("/post/like")
public ResponseEntity<?> updateLike(@RequestBody SetLikeDto.Request postLikeDto, @AuthenticationPrincipal UserDetailsImpl userDetails){
Post post = postService.setPostLike(postLikeDto,userDetails.getUser());
SetLikeDto.Response response = modelMapper.map(post, SetLikeDto.Response.class);
return ResponseEntity.ok(response);
}
@PostMapping("/post/like")
public ResponseEntity<Objects> updateLike(@RequestBody SetLikeDto.Request postLikeDto, @AuthenticationPrincipal UserDetailsImpl userDetails){
Post post = postService.setPostLike(postLikeDto,userDetails.getUser());
SetLikeDto.Response response = modelMapper.map(post, SetLikeDto.Response.class);
return ResponseEntity.ok(response);
}
URI
URL
URL 과 URI의 차이 예시
REST에서 가장 중요한 규칙
예시