ResponseEntity가 담는 것 3가지
ResponseEntity<T>
- 상태 코드 (200, 401, 204, 500 등)
- 응답 헤더 (Set-Cookie, Authorization 등)
- 응답 바디 (JSON 데이터, 없을 수도 있음)
예제
200 OK + 데이터
return ResponseEntity.ok(userDto);
204 No Content
return ResponseEntity.noContent().build();
401 Unauthorized
return ResponseEntity.status(401).build();
예외 처리랑 붙였을 때 완성형
@PostMapping("/login")public Tokenslogin(...) {if (bad)thrownewUnauthorizedException();return tokens;
}
@RestControllerAdviceclassGlobalHandler {@ExceptionHandler(UnauthorizedException.class)@ResponseStatus(HttpStatus.UNAUTHORIZED)voidhandle() {}
}
상태코드 작성