简体   繁体   中英

javax servlet filter is not working with Mono reactor

Hi I have created a spring boot application in which I am using Mono reactive. Now I wanted to do some logging before and after request.. For that I am using javax.servelet.filter like this

@Component
@Order(1)
public class RequestLoggingFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest= (HttpServletRequest) request;
        HttpServletResponse httpRespnse = (HttpServletResponse) response;

        new CustomLogger().getColorLogger().info("dsfsdfsf");
        chain.doFilter(request, response);
        new CustomLogger().getColorLogger().info("sdfsdfsfsdf");
    }
}

But both logs are getting printed simultaneously... then my execution starts. I don't know what I am doing wrong. Can anyone help me?

I'm supposed you use Spring Webflux here. Spring webflux comes with his own filter mechanism.

You could use org.springframework.web.server.WebFilter

Example

@Component
public class DemoFilter implements WebFilter {

    @Override
    public Mono<Void> filter(ServerWebExchange serverWebExchange, 
      WebFilterChain webFilterChain) {

        // filter logic here.
        return webFilterChain.filter(serverWebExchange);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM