I have the following global request mapping to handle all non existing RESTs:
@RequestMapping("*")
@ResponseStatus(HttpStatus.NOT_FOUND)
public ErrorResponse handlerNotMappingRequest(RequestContext context) {
return new ErrorResponse("Path not found: '" + context.getPath() +"'", HttpStatus.NOT_FOUND);
}
But this disables the: /swagger-ui.html
API documentation.
When I remove the global mapping everything works fine, but I loose my custom 404 not found response.
PS: I have tried to implement a global 404 exception handler via NoHandlerFoundException.class
and configured the DispatcherServlet
.setThrowExceptionIfNoHandlerFound(true)
... but it didn't work ... the exception never got triggered.
So is there a way around this?
- Either configuring my spring boot app to know it should react to swagger requests?
- Override the global 404 exception handling with my own?
I have tried the following:
@Configuration
@SpringBootApplication
public class CatalogApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(MyApplication.class, args);
DispatcherServlet dispatcherServlet = (DispatcherServlet)ctx.getBean("dispatcherServlet");
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
}
But the no handler found exception is not thrown!