I have the below controller which returns a Mono of string
@RestController
@RequestMapping("api/v1/test")
public class TestController {
@PostMapping
public Mono<String> getDraft() {
return Mono.just("ok");
}
}
I added the bean WebFilter to do some treatment when a request comes in, the problem is that the message in the bean does not display in the console, i tried to add a breakpoint to debug but when i test the api it's not stopping in the breakpoint. In the actuator/beans i found the bean slf4jMdcFilter. There's another config to add ?
@Configuration
public class WebConfig {
public static final String TRX_ID = "transactionId";
public static final String PATH_URI = "pathUri";
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
WebFilter slf4jMdcFilter() {
return (exchange, chain) -> {
System.out.println("Filtering request");
String requestId = exchange.getRequest().getId();
return chain.filter(exchange)
.contextWrite(Context.of(TRX_ID, requestId)
.put(PATH_URI, exchange.getRequest().getPath()));
};
}
}
I have experienced the same profblem with this code. The cause of my problem was that my application was an mvc and not a WebFlux application. Spring boot creates all beans at startup thats why you can see the bean in actuator. The cause your filter is not being triggered is probably the type of your application. You can't mix mvc with webflux. Spring MVC works very well with Mono and Flux but this is still a mvc application. That's why filters are not triggered. Try to implement an interceptor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With