2016-08-03 91 views
7

我正在用Spring Boot创建一个API,所以希望禁用/error映射。Spring Boot Disable/error mapping

我已经设置了以下道具application.properties:

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

然而,当我打/error我得到:

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"} 

所需的结果

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

回答

15

你可以禁用ErrorMvcAutoConfiguration:

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

或者通过春季启动的application.yml /属性:

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

如果这不是一个选择,你也可以用你自己的实现扩展Spring的ErrorController:

@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

是的,我做了这个确切的事情,它只是烦人!不知道是否有办法删除绑定而不这样做 – ptimson

+0

同样,如果我删除它所有的错误现在是html而不是json ... – TheBakker

0

在我的情况下,问题在于登录页面标题中引用的Web资源。具体来说,css在标题中被引用,但实际上并不存在于项目中。

在我的WebSecurityConfigurerAdapter实现中,我首先注释了configure(WebSecurity web)的主体,然后尝试登录,而不是显示上述错误,但我的浏览器的地址栏会显示导致问题的资源的url 。