|
77 | 77 | import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
78 | 78 | import org.springframework.web.accept.ContentNegotiationManager;
|
79 | 79 | import org.springframework.web.accept.ParameterContentNegotiationStrategy;
|
| 80 | +import org.springframework.web.bind.annotation.ControllerAdvice; |
80 | 81 | import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
|
81 | 82 | import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
82 | 83 | import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
|
106 | 107 | import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver;
|
107 | 108 | import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
|
108 | 109 | import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
| 110 | +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; |
109 | 111 | import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
|
110 | 112 | import org.springframework.web.servlet.resource.CachingResourceResolver;
|
111 | 113 | import org.springframework.web.servlet.resource.CachingResourceTransformer;
|
@@ -959,6 +961,25 @@ void addResourceHandlersAppliesToChildAndParentContext() {
|
959 | 961 | }
|
960 | 962 | }
|
961 | 963 |
|
| 964 | + @Test |
| 965 | + void problemDetailsDisabledByDefault() { |
| 966 | + this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(ProblemDetailsExceptionHandler.class)); |
| 967 | + } |
| 968 | + |
| 969 | + @Test |
| 970 | + void problemDetailsEnabledAddsExceptionHandler() { |
| 971 | + this.contextRunner.withPropertyValues("spring.mvc.problemdetails.enabled:true") |
| 972 | + .run((context) -> assertThat(context).hasSingleBean(ProblemDetailsExceptionHandler.class)); |
| 973 | + } |
| 974 | + |
| 975 | + @Test |
| 976 | + void problemDetailsBacksOffWhenExceptionHandler() { |
| 977 | + this.contextRunner.withPropertyValues("spring.mvc.problemdetails.enabled:true") |
| 978 | + .withUserConfiguration(CustomExceptionResolverConfiguration.class) |
| 979 | + .run((context) -> assertThat(context).doesNotHaveBean(ProblemDetailsExceptionHandler.class) |
| 980 | + .hasSingleBean(CustomExceptionResolver.class)); |
| 981 | + } |
| 982 | + |
962 | 983 | private void assertResourceHttpRequestHandler(AssertableWebApplicationContext context,
|
963 | 984 | Consumer<ResourceHttpRequestHandler> handlerConsumer) {
|
964 | 985 | Map<String, Object> handlerMap = getHandlerMap(context.getBean("resourceHandlerMapping", HandlerMapping.class));
|
@@ -1485,4 +1506,19 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
1485 | 1506 |
|
1486 | 1507 | }
|
1487 | 1508 |
|
| 1509 | + @Configuration(proxyBeanMethods = false) |
| 1510 | + static class CustomExceptionResolverConfiguration { |
| 1511 | + |
| 1512 | + @Bean |
| 1513 | + CustomExceptionResolver customExceptionResolver() { |
| 1514 | + return new CustomExceptionResolver(); |
| 1515 | + } |
| 1516 | + |
| 1517 | + } |
| 1518 | + |
| 1519 | + @ControllerAdvice |
| 1520 | + static class CustomExceptionResolver extends ResponseEntityExceptionHandler { |
| 1521 | + |
| 1522 | + } |
| 1523 | + |
1488 | 1524 | }
|
0 commit comments