1

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?

  1. Either configuring my spring boot app to know it should react to swagger requests?
  2. 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!

Drejc
  • 14,196
  • 16
  • 71
  • 106
  • http://stackoverflow.com/a/36139231/3641067 ? – buræquete Mar 31 '17 at 07:29
  • Just configure it properly. How are you setting the `throwExceptionIfNoHandlerFound` property. You shouldn't be messing around with the servlet yourself (ideally) and only set this property in the `application.properties`. – M. Deinum Mar 31 '17 at 08:01

0 Answers0