Java Spring Boot Disable /error mapping
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38747548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot Disable /error mapping
提问by ptimson
I am creating an API with Spring Boot so wish to disable the /error
mapping.
I am creating an API with Spring Boot so wish to disable the /error
mapping.
I have set the following props in application.properties:
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:
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
Required Result
HTTP/1.1 404 Internal Server Error
Server: Apache-Coyote/1.1
采纳答案by alexbt
You can disable the ErrorMvcAutoConfiguration :
You can disable the ErrorMvcAutoConfiguration :
@SpringBootApplication
@EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class})
public class SpringBootLauncher {
Or through Spring Boot's application.yml/properties:
Or through Spring Boot's application.yml/properties:
spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
If this is not an option for you, you may also extend Spring's ErrorController with your own implementation:
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;
}
Note:Use oneof the above techniques (disabled auto-configuration or implement the error-controller). Both together will not work, as mentioned in the comments.
Note:Use oneof the above techniques (disabled auto-configuration or implement the error-controller). Both together will not work, as mentioned in the comments.
回答by Dmitry
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.
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.
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.
回答by kejm
Attributes should be specified via @SpringBootApplication. Example in Kotlin:
Attributes should be specified via @SpringBootApplication. Example in Kotlin:
@SpringBootApplication(exclude = [ErrorMvcAutoConfiguration::class])
class SpringBootLauncher {