-
Spring Boot : ProblemDetailsExceptionHandlerSpring Boot 2024. 4. 10. 18:55
ProblemDetail
ProblemDetail은 Spring Framework 6.0에 추가된 REST API의 에러 응답 표준이다.
RFC 7807에 정의되어 있다.- 참고: https://docs.spring.io/spring-framework/docs/6.0.7/reference/html/web.html#mvc-ann-rest-exceptions
아래는 간단히 Spring Boot의 기본 에러 응답을 ProblemDetail로 설정하는 방법을 설명한다.
ProblemDetailsExceptionHandler
에러 응답 예시
아래는 Spring Boot의 기본 에러 응답을 ProblemDetail로 설정했을 때의 에러 응답 body이다.
{ "type": "about:blank", "title": "Method Not Allowed", "status": 405, "detail": "Method 'POST' is not supported.", "instance": "/" }
Spring Boot 설정
아래 설정을 추가하면 ProblemDetailsExceptionHandler가 자동 구성된다.
이를 통해 Spring Framework에서 제공하는 웹 예외들은 자동으로 ProblemDetail 형식의 response body로 에러 응답을 한다.spring.mvc.problemdetails.enabled=true
자동 구성
위의 설정이 되어있으면 아래 클래스에서 ProblemDetailsExceptionHandler가 자동 구성된다.
package org.springframework.boot.autoconfigure.web.servlet; public class WebMvcAutoConfiguration { @Configuration(proxyBeanMethods = false) @ConditionalOnProperty(prefix = "spring.mvc.problemdetails", name = "enabled", havingValue = "true") static class ProblemDetailsErrorHandlingConfiguration { @Bean @ConditionalOnMissingBean(ResponseEntityExceptionHandler.class) @Order(0) ProblemDetailsExceptionHandler problemDetailsExceptionHandler() { return new ProblemDetailsExceptionHandler(); } } }
자동 구성되는 ProblemDetailsExceptionHandler
ResponseEntityExceptionHandler를 확장한 구현 클래스일 뿐이다.
package org.springframework.boot.autoconfigure.web.servlet; @ControllerAdvice class ProblemDetailsExceptionHandler extends ResponseEntityExceptionHandler { }
'Spring Boot' 카테고리의 다른 글
Spring Framework의 테스트 클래스에서 단일 생성자로 의존성 자동 주입이 되지 않는 이유 (1) 2024.10.09 Spring Boot JPA : Java 날짜/시간 데이터 타입과 JPA, MySQL 타임존의 관계 (0) 2024.04.11 Spring Boot : Default Error Response : 기본 에러 응답 (0) 2024.04.10