Spring Boot Disable /error mapping

匿名 (未验证) 提交于 2019-12-03 02:24:01

问题:

I am creating an API with Spring Boot so wish to disable the /error mapping.

I have set the following props in application.properties:

server.error.whitelabel.enabled=false spring.mvc.throw-exception-if-no-handler-found=true spring.resources.add-mappings=false 

However when I hit /error I get:

HTTP/1.1 500 Internal Server Error Server: Apache-Coyote/1.1 Content-Type: application/json;charset=UTF-8 Transfer-Encoding: chunked Date: Wed, 03 Aug 2016 15:15:31 GMT Connection: close  {"timestamp":1470237331487,"status":999,"error":"None","message":"No message available"} 

Required Result

HTTP/1.1 404 Internal Server Error Server: Apache-Coyote/1.1 

回答1:

You can disable the ErrorMvcAutoConfiguration :

@SpringBootApplication @EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class}) public class SpringBootLauncher { 

Or through Spring Boot's application.yml/properties:

spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration 

If this is not an option for you, you may also extend Spring's ErrorController with your own implementation:

@RestController public class MyErrorController implements ErrorController {      private static final String ERROR_MAPPING = "/error";      @RequestMapping(value = ERROR_MAPPING)     public ResponseEntity<String> error() {         return new ResponseEntity<String>(HttpStatus.NOT_FOUND);     }      @Override     public String getErrorPath() {         return ERROR_MAPPING;     } 


回答2:

In my case the problem was with web resources referenced in the header of the login page. Specifically, css was referenced in the header, but did not actually exist in the project.

What might also be helpful, in my WebSecurityConfigurerAdapter implementation I commented out the body of configure(WebSecurity web) first, then on trying to login, instead of displaying the above error json my browser's address bar would display the url to the resource causing the problem.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!