@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ResponseBody {
public class ResponseEntity<T> extends HttpEntity<T> {
private final Object status;
HttpEntity class를 상속받은 class이면서 상태 코드가 붙어있는 class
- POST 요청 -> HandlerAdapter로 요청 들어옴
POST /reservations 1.1 ... date : "2024-05-16" themeId: "3" timeId : "3"
- Argument Resolver가 컨트롤러에 맞는 파라미터값 생성 -> Handler로 넘겨줌
- ReservationRequestDTO 를 만들어서 Handler로 넘겨줌
- Handler에서는 로직 처리 후 응답값이 있으면 ReturnValueHandler로 전달
@Nullable
private HandlerMethodReturnValueHandler selectHandler(@Nullable Object value, MethodParameter returnType) {
boolean isAsyncValue = isAsyncReturnValue(value, returnType);
// 여러 종류의 ReturnValueHandler 구현체
for (HandlerMethodReturnValueHandler handler : this.returnValueHandlers) {
if (isAsyncValue && !(handler instanceof AsyncHandlerMethodReturnValueHandler)) {
continue;
}
if (handler.supportsReturnType(returnType)) {
return handler;
}
}
return null;
}
RequestResponseBodyMethodProcessor
라는 구현체HttpEntityMethodProcessor
라는 구현체@RestController
@RequiredArgsConstructor
public class ReservationController {
private final ReservationService reservationService;
@GetMapping("/reservations")
public ResponseEntity<List<ReservationResponse>> findAllReservations() {
return ResponseEntity.ok(reservationService.findAll());
}
@PostMapping("/reservations")
public ResponseEntity<ReservationResponse> createReservation (@RequestBody reservationCreateMemberRequest memberReqeust, AuthInfo authInfo) {
ReservationCreateRequest reservationCreateRequest = ReservationCreateRequst.from(memberRequest, authInfo.id());
ReservationResponse reservationResponse = reservationService.save(reservationCreateRequest);
return ResponseEntity.created(URI.create("admin/reservation")).body(reservationResponse);
...
편의성 & 동적 응답 지정
@ResponseStatus 사용하면 정적인 특성을 가짐
@GetMapping("/login/check")
public ResponseEntity<MemberResponse> checkLogin(AutoInfo authInfo) {
if(authInfo == null) {
return ResponseEntity.noContent().build();
}
MemberResponse memberResponse = MemberResponse.from(authInfo);
return ResponseEntity.ok(memberResponse);
}