Today I've spent too much time writing a middleware in kotlin for spring webflux to log my request. As I am super new, It was pretty difficult to understand some of the complex examples on few tutorial found online.

So I wanted to share the basic template for writing a middleware using WebFilter.

Middleware Template Example

@Component
class LoggingFilter : WebFilter {
    override fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> {
        
        // your code here
        // this code will be executed before your controller
  
        return chain.filter(exchange)
    }
}

Do not forget to use @Component which is used to auto wire your filter / middleware.