Add the following dependencies to your project:
<dependency>
<groupId>org.zalando</groupId>
<artifactId>problem-spring-webflux</artifactId>
<version>${problem-spring-webflux.version}</version>
</dependency>
<dependency>
<groupId>org.zalando</groupId>
<artifactId>jackson-datatype-problem</artifactId>
<version>0.27.1</version>
</dependency>
Make sure you register the required modules with your ObjectMapper:
@Bean
public ProblemModule problemModule() {
return new ProblemModule();
}
@Bean
public ConstraintViolationProblemModule constraintViolationProblemModule() {
return new ConstraintViolationProblemModule();
}
The following table shows all built-in advice traits:
You're free to use them either individually or in groups. Future versions of this library may add additional traits to groups. A typical usage would look like this:
@ControllerAdvice
class ExceptionHandling implements ProblemHandling {
}
In WebFlux, if a request handler is not called, then the ControllerAdvice
will not be used. So for
ResponseStatusAdviceTrait
for a 404 Not found
,
MethodNotAllowedAdviceTrait
,
NotAcceptableAdviceTrait
,
and UnsupportedMediaTypeAdviceTrait
it is required to add a specific WebExceptionHandler
:
@Bean
@Order(-2) // The handler must have precedence over WebFluxResponseStatusExceptionHandler and Spring Boot's ErrorWebExceptionHandler
public WebExceptionHandler problemExceptionHandler(ObjectMapper mapper, ProblemHandling problemHandling) {
return new ProblemExceptionHandler(mapper, problemHandling);
}
The Spring Security integration requires additional steps:
@ControllerAdvice
class ExceptionHandling implements ProblemHandling, SecurityAdviceTrait {
}
@Configuration
@Import(SecurityProblemSupport.class)
public class SecurityConfiguration {
@Autowired
private SecurityProblemSupport problemSupport;
@Bean
public SecurityWebFilterChain securityWebFilterChain(final ServerHttpSecurity http) {
return http.exceptionHandling()
.authenticationEntryPoint(problemSupport)
.accessDeniedHandler(problemSupport)
.and().build();
}
}
SecurityProblemSupport
will need a SecurityAdviceTrait
bean at startup. For instance:
@ControllerAdvice
public class SecurityExceptionHandler implements SecurityAdviceTrait {
}
The optional failsafe integration adds support for CircuitBreakerOpenException
in the form of an advice trait:
@ControllerAdvice
class ExceptionHandling implements ProblemHandling, CircuitBreakerOpenAdviceTrait {
}
An open circuit breaker will be translated into a 503 Service Unavailable
:
HTTP/1.1 503 Service Unavailable
Content-Type: application/problem+json
{
"title": "Service Unavailable",
"status": 503
}
The optional integration for Atlassian's Swagger Request Validator adds support for invalid request/response exceptions as a dedicated advice trait:
@ControllerAdvice
class ExceptionHandling implements ProblemHandling, OpenApiValidationAdviceTrait {
}