Spring Webflux — wrap response
Spring webflux — the reactive universe of the Spring framework is very interesting place.
This post is dedicated to wrapping around the response to a common response that solves the purpose of having common fields which are always required for all API endpoints.
The code repository is here — https://github.com/krnbr/spring-webflux-wrap-response
Here I make use of the ResponseBodyResultHandler to wrap the request inside a common structure of the response if the api mapping method or the controller class is mapped with a specific @ApiResponse annotation.
The example code in the repository shared above works well in the case of the Mono and non Mono wrapped objects, for the Flux based objects there will be required some additional changes.
The main code is in the class — CustomResponseBodyResultHandler
@Slf4j
public class CustomResponseBodyResultHandler extends ResponseBodyResultHandler {
public CustomResponseBodyResultHandler(List<HttpMessageWriter<?>> writers, RequestedContentTypeResolver resolver) {
super(writers, resolver);
}
@Override
public boolean…