Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am looking for servlet Filter equivalent in Spring WebFlux. The WebFilter seems to only fires before the controller but not after. e.g I can add a WebFilter to do something when a request comes in, but I couldn't find an equivalent "filter" to do something when a response is sending back.

Can you have a "filter" that fires in both ways?

Does this answer your question? How to intercept a request when using SpringBoot WebClient dekkard Jun 1, 2022 at 7:48 @dekkard Sorry no, if I understand it correctly, that one you posted is for a WebClient , while I am after something for my server user1589188 Jun 1, 2022 at 8:05

You can still use WebFilter to modify your server's outbound responses as well. Here's an example of adding a header to the response:

@Component
public class ExampleWebFilter implements WebFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange serverWebExchange, 
      WebFilterChain webFilterChain) {
        serverWebExchange.getResponse()
          .getHeaders().add("web-filter", "web-filter-test");
        return webFilterChain.filter(serverWebExchange);

Reference: https://www.baeldung.com/spring-webflux-filters

Yes I know it can modifies both the request and the response, but the issue is WebFilter#filter() happens only before the actual controller, but not after. – user1589188 Jun 1, 2022 at 8:24 That is no so for webflux. In any case I don't see why such internal implementation details even matter when this clearly does what you requested. – dekkard Jun 1, 2022 at 9:11 If it doesn't fire after the controller, how do you act according to the response body sets by the controller? – user1589188 Jun 1, 2022 at 9:15 In Webflux there's a single stream between a client and a server with data flowing both ways. As soon as we read the request or response body for processing, the input stream is consumed, so the controller or client doesn’t receive the body. So intercepting a request or response body in Webflux is not a trivial task. Have a look at stackoverflow.com/a/45280764/4571544 and stackoverflow.com/questions/61706948/… for more info. – dekkard Jun 1, 2022 at 9:55

Just add code after the webFilterChain.filter call.

@Component
public class MyFilter implements WebFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
  Mono<Void> result = chain.filter(exchange);
  return result.then(<do-whatever>);
                Haha yeah nah, this just runs code after the other filters, not after controller during the outbound.
– user1589188
                Jun 1, 2022 at 10:24
                No it doesn't. The controller will be called after all filters processed and then it goes back up. At least it should.
– M. Deinum
                Jun 1, 2022 at 11:05
                By definition WebFilter is executed before controller. You could look at special beans like HandlerResultHandler or consider function WebFlux endpoints that you could use to wrap handler and implement own logic.
– Alex
                Jun 1, 2022 at 14:37
                > Haha yeah nah, this just runs code after the other filters, not after controller during the outbound  According to your tone, may we assume that you have indeed tested and verified it?
– BalusC
                Jun 2, 2022 at 13:20

I think the trick you are looking for is when you call the filter chain in the filter method you want the following

@Override
public Mono<Void> filter(@NonNull final ServerWebExchange exchange, @NonNull final WebFilterChain chain) {
        final ServerHttpRequest request = exchange.getRequest();
        final String requestUrl = request.getURI().toString();
        // preRequest: - YOUR LOGIC
        return chain.filter(exchange).doFinally(signalType -> {
            /* This doFinally is included so the tests will pass */
            log.info("postHandle: Just like an interceptor");
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.