东西(Tomcat?)取代了我的Rest响应

时间:2014-04-16 11:59:31

标签: java spring tomcat spring-mvc spring-boot

我有一个简单的spring-boot网络应用程序使用json进行通信,这是完美的。

然后我决定通过引入一个带有一些异常处理程序方法(正确调用)的全局@ControllerAdvice注释类来清理错误处理(在阅读this之后)但在某处将响应转换为Tomcat标准错误而不是我想要的RestErrorInfo。

我为全局错误处理添加的代码:

@ControllerAdvice
public class GlobalDefaultExceptionHandler {
    @ResponseStatus(HttpStatus.CONFLICT)
    @ExceptionHandler(DataIntegrityViolationException.class)
    public RestErrorInfo handleSpringDataConflict() {
        return new RestErrorInfo(HttpStatus.CONFLICT, "database problem");
    }
    ...
}

我希望在主体中得到一个带有RestErrorInfo的409,但我得到的响应是:

POST http://localhost:8080/rest/books 405 (Method Not Allowed) :8080/rest/books:1
HTTP 405: <html><head><title>Apache Tomcat/7.0.52 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 405 - Request method 'POST' not supported</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>Request method 'POST' not supported</u></p><p><b>description</b> <u>The specified HTTP method is not allowed for the requested resource.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.52</h3></body></html>

如果我发布允许的内容,应用程序可以正常返回200(将很快更改为201 ;-)和Rest对象。

我正在使用:  春天4.0.3.RELEASE  spring-boot 1.0.1.RELEASE  spring-security 3.2.1.RELEASE

另一件事可能会搞砸它:我使用过滤器在标题中设置Access-Control-Allow-Origin。

@Component
public class SimpleCORSFilter implements Filter {

    public static final List<String> ALL_METHODS = Arrays.asList("OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "CONNECT");

    public void doFilter(@NotNull final ServletRequest req, @NotNull final ServletResponse res, @NotNull final FilterChain chain) throws IOException, ServletException {
    final HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", String.join(", ", ALL_METHODS));
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with, Content-Type, accept");
        chain.doFilter(req, res);
    }
    public void init(@NotNull final FilterConfig filterConfig) {}
    public void destroy() {}
}

1 个答案:

答案 0 :(得分:3)

将代码更改为以下内容:

@ResponseStatus(HttpStatus.CONFLICT)
@ExceptionHandler(DataIntegrityViolationException.class)
@ResponseBody
public RestErrorInfo handleSpringDataConflict() {
     return new RestErrorInfo(HttpStatus.CONFLICT, "database problem");
}