{"action":"opened","issue":{"url":"https://api.github.com/repos/spring-projects/spring-boot/issues/3980","labels_url":"https://api.github.com/repos/spring-projects/spring-boot/issues/3980/labels{/name}","comments_url":"https://api.github.com/repos/spring-projects/spring-boot/issues/3980/comments","events_url":"https://api.github.com/repos/spring-projects/spring-boot/issues/3980/events","html_url":"https://github.com/spring-projects/spring-boot/issues/3980","id":107214457,"number":3980,"title":"DispatcherServlet#setThrowExceptionIfNoHandlerFound doesn't work for REST applicaiton","user":{"login":"igilham","id":541775,"avatar_url":"https://avatars.githubusercontent.com/u/541775?v=3","gravatar_id":"","url":"https://api.github.com/users/igilham","html_url":"https://github.com/igilham","followers_url":"https://api.github.com/users/igilham/followers","following_url":"https://api.github.com/users/igilham/following{/other_user}","gists_url":"https://api.github.com/users/igilham/gists{/gist_id}","starred_url":"https://api.github.com/users/igilham/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/igilham/subscriptions","organizations_url":"https://api.github.com/users/igilham/orgs","repos_url":"https://api.github.com/users/igilham/repos","events_url":"https://api.github.com/users/igilham/events{/privacy}","received_events_url":"https://api.github.com/users/igilham/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-09-18T14:32:27Z","updated_at":"2015-09-18T14:32:27Z","closed_at":null,"body":"This issue occurs in Spring Boot 1.1.12, 1.2.0, and 1.2.5. There are a few related answers on StackOverflow but none of them seem to work.\r\n\r\nI'm trying to override the error handling behaviour to return a json-encoded custom POJO to the user when exceptions are thrown by the controllers in my application. This works for most exceptions but I can't get it to work with for 404s where there is no handler matching the path of the request.\r\n\r\nOverriding the `DispatcherServlet` and setting `setThrowExceptionIfNoHandlerFound` to `true` does not result in the appropriate exception handler being called.\r\n\r\nHere is my `DispatcherServlet` Bean declaration:\r\n\r\n```java\r\n@Configuration\r\npublic class WebAppContext extends WebMvcConfigurerAdapter {\r\n\r\n    @Bean(name = DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)\r\n    public DispatcherServlet dispatcherServlet() {\r\n        DispatcherServlet ds = new DispatcherServlet();\r\n        ds.setDetectAllHandlerAdapters(true);\r\n        ds.setDetectAllHandlerExceptionResolvers(true);\r\n        ds.setDetectAllHandlerMappings(true);\r\n        ds.setDetectAllViewResolvers(true);\r\n        ds.setThrowExceptionIfNoHandlerFound(true);\r\n        return ds;\r\n    }\r\n\r\n// ... @ExceptionHandler methods etc.\r\n}\r\n```\r\n\r\nAnd here's part of the global Exception handler:\r\n\r\n```java\r\n@ControllerAdvice(annotations = RestController.class)\r\npublic class GlobalExceptionHandler extends ResponseEntityExceptionHandler {\r\n\r\n    // TODO: figure out how to configure the DispatcherServlet's throwExceptionIfNoHandlerFound property so that this can work\r\n    @Override\r\n    protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {\r\n        return new ResponseEntity<>(new CustomErrorType(HttpStatus.NOT_FOUND.value(), \"Not Found\"), HttpStatus.NOT_FOUND);\r\n    }\r\n\r\n// ... more Bean definitions\r\n}\r\n```\r\n\r\nI have also tried using the `BeanPostProcessor` interface to customise the `DispatcherServlet` as noted in a comment on an answer to [this question](stackoverflow.com/questions/28902374/spring-boot-rest-service-exception-handling), but that doesn't work either.\r\n\r\nThe documentation for how to handle global exceptions and customise the `DispatcherServlet` is incredibly obtuse. Am I missing something or is there a bug in there somewhere?"}}