How to throw exception if no handler found in Webflux?

Comment From: jaDEVirek

Hi @alxxyz

By default, the DispatcherServlet does not throw a NoHandlerFoundException. You need to enable that:

Check if you have right configuration in your application.properties. spring.mvc.throw-exception-if-no-handler-found=true

Besides, if you are using @EnableWebMvc then it is likely that your changes made, for example, in properties files will be overwritten. https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-servlet-config

Comment From: alxxyz

I am using Webflux instead of WebMvc, does it work also for Webflux?

Comment From: rstoyanchev

@alxxyz that should be the case out of the box in WebFlux, in DispatcherHandler.

Comment From: alxxyz

@rstoyanchev can you please advise how I can catch it and override the response? As I can not catch it in the @ControllerAdvice

Comment From: rstoyanchev

@alxxyz, catching early exceptions from before a handler is chosen was added recently for 6.0 with #22991, so it's not supported in 5.3.x. Are you trying to add a body to the error response or something else?

Comment From: alxxyz

Yes, we have an API contract and I need to send a custom body

Comment From: rstoyanchev

I see, so that's supported from 6.0 onwards. In 5.3.x, you can use a WebExceptionHandler as a workaround, or Boot's error handling.

Comment From: alxxyz

Thank you @rstoyanchev