Skip to content

Latest commit

 

History

History

problem-spring-webflux

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Problem: Spring WebFlux

Javadoc Maven Central

Installation

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>

Configuration

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:

Advice Trait Produces
ProblemHandling
├──GeneralAdviceTrait
│   ├──ProblemAdviceTrait depends
│   ├──ThrowableAdviceTrait 500 Internal Server Error
│   └── UnsupportedOperationAdviceTrait 501 Not Implemented
├──HttpAdviceTrait
│   ├──MethodNotAllowedAdviceTrait 405 Method Not Allowed
│   ├──NotAcceptableAdviceTrait 406 Not Acceptable
│   ├──ResponseStatusAdviceTrait
│   └──UnsupportedMediaTypeAdviceTrait 415 Unsupported Media Type
├──NetworkAdviceTrait
│   └──SocketTimeoutAdviceTrait 504 Gateway Timeout
└──ValidationAdviceTrait
    └──ConstraintViolationAdviceTrait 400 Bad Request

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);
}

Security

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 {
}

Failsafe

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
}

Swagger/OpenAPI Request Validator

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 {

}