Skip to content

Order ProblemDetailsExceptionHandler beans #36288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ static class ProblemDetailsErrorHandlingConfiguration {

@Bean
@ConditionalOnMissingBean(ResponseEntityExceptionHandler.class)
@Order(0)
ProblemDetailsExceptionHandler problemDetailsExceptionHandler() {
return new ProblemDetailsExceptionHandler();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ static class ProblemDetailsErrorHandlingConfiguration {

@Bean
@ConditionalOnMissingBean(ResponseEntityExceptionHandler.class)
@Order(0)
ProblemDetailsExceptionHandler problemDetailsExceptionHandler() {
return new ProblemDetailsExceptionHandler();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import jakarta.validation.ValidatorFactory;
import org.aspectj.lang.JoinPoint;
Expand Down Expand Up @@ -77,6 +78,7 @@
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.filter.reactive.HiddenHttpMethodFilter;
import org.springframework.web.method.ControllerAdviceBean;
import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
import org.springframework.web.reactive.config.DelegatingWebFluxConfiguration;
Expand Down Expand Up @@ -667,6 +669,24 @@ void problemDetailsBacksOffWhenExceptionHandler() {
.hasSingleBean(CustomExceptionHandler.class));
}

@Test
void problemDetailsIsOrderedBetweenLowestAndHighestOrderedControllerHandlers() {
this.contextRunner.withPropertyValues("spring.webflux.problemdetails.enabled:true")
.withUserConfiguration(OrderedControllerAdviceBeansConfiguration.class)
.run((context) -> {

List<Class<?>> controllerAdviceClasses = ControllerAdviceBean.findAnnotatedBeans(context)
.stream()
.map(ControllerAdviceBean::getBeanType)
.collect(Collectors.toList());

assertThat(controllerAdviceClasses).containsExactly(
OrderedControllerAdviceBeansConfiguration.HighestOrderedControllerAdvice.class,
ProblemDetailsExceptionHandler.class,
OrderedControllerAdviceBeansConfiguration.LowestOrderedControllerAdvice.class);
});
}

private ContextConsumer<ReactiveWebApplicationContext> assertExchangeWithSession(
Consumer<MockServerWebExchange> exchange) {
return (context) -> {
Expand Down Expand Up @@ -971,6 +991,25 @@ static class CustomExceptionHandler extends ResponseEntityExceptionHandler {

}

@Configuration(proxyBeanMethods = false)
@Import({ OrderedControllerAdviceBeansConfiguration.LowestOrderedControllerAdvice.class,
OrderedControllerAdviceBeansConfiguration.HighestOrderedControllerAdvice.class })
static class OrderedControllerAdviceBeansConfiguration {

@ControllerAdvice
@Order
static class LowestOrderedControllerAdvice {

}

@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
static class HighestOrderedControllerAdvice {

}

}

@Aspect
static class ExceptionHandlerInterceptor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand All @@ -51,6 +52,8 @@
import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration;
import org.springframework.boot.autoconfigure.validation.ValidatorAdapter;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfigurationTests.OrderedControllerAdviceBeansConfiguration.HighestOrderedControllerAdvice;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfigurationTests.OrderedControllerAdviceBeansConfiguration.LowestOrderedControllerAdvice;
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
Expand All @@ -65,6 +68,8 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
Expand All @@ -89,6 +94,7 @@
import org.springframework.web.filter.FormContentFilter;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.filter.RequestContextFilter;
import org.springframework.web.method.ControllerAdviceBean;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.FlashMap;
import org.springframework.web.servlet.FlashMapManager;
Expand Down Expand Up @@ -972,6 +978,22 @@ void problemDetailsBacksOffWhenExceptionHandler() {
.hasSingleBean(CustomExceptionHandler.class));
}

@Test
void problemDetailsIsOrderedBetweenLowestAndHighestOrderedControllerHandlers() {
this.contextRunner.withPropertyValues("spring.mvc.problemdetails.enabled:true")
.withUserConfiguration(OrderedControllerAdviceBeansConfiguration.class)
.run((context) -> {

List<Class<?>> controllerAdviceClasses = ControllerAdviceBean.findAnnotatedBeans(context)
.stream()
.map(ControllerAdviceBean::getBeanType)
.collect(Collectors.toList());

assertThat(controllerAdviceClasses).containsExactly(HighestOrderedControllerAdvice.class,
ProblemDetailsExceptionHandler.class, LowestOrderedControllerAdvice.class);
});
}

private void assertResourceHttpRequestHandler(AssertableWebApplicationContext context,
Consumer<ResourceHttpRequestHandler> handlerConsumer) {
Map<String, Object> handlerMap = getHandlerMap(context.getBean("resourceHandlerMapping", HandlerMapping.class));
Expand Down Expand Up @@ -1496,6 +1518,24 @@ CustomExceptionHandler customExceptionHandler() {

}

@Configuration(proxyBeanMethods = false)
@Import({ LowestOrderedControllerAdvice.class, HighestOrderedControllerAdvice.class })
static class OrderedControllerAdviceBeansConfiguration {

@ControllerAdvice
@Order
static class LowestOrderedControllerAdvice {

}

@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
static class HighestOrderedControllerAdvice {

}

}

@ControllerAdvice
static class CustomExceptionHandler extends ResponseEntityExceptionHandler {

Expand Down