Back-End/SpringBoot
[SpringBoot] Exception Handler에 return 값을 ResponseEntity로 하지 않으면?
코딩콩
2022. 12. 14. 14:17
ExceptionHandler 코드를 작성하는 도중 의문점이 들었다.
왜 꼭 "ResponseEntity"로 return 해야 하는지. 그냥 "ErrorResponse"로 해도 같은 형태로 응답이 오던데,,,
문제는 바로 HttpStatus였다.
Error인데 200으로 응답되더라!
ResponseEntity에 status 잘해서 넘겨주기.
@ExceptionHandler(CommonException.class)
public ResponseEntity customJwtException(CommonException e) {
e.printStackTrace();
log.warn("CommonException : {}", e.getResponseCode().getDescription());
return ResponseEntity
.status(e.getResponseCode().getHttpStatus())
.body(new ErrorResponse(e.getResponseCode()));
}