【问题标题】:Something (Tomcat?) replaces my Rest repsonse某些东西(Tomcat?)取代了我的 Rest 响应
【发布时间】:2014-05-31 06:57:31
【问题描述】:

我有一个使用 json 进行通信的简单 spring-boot web-app,它运行良好。

然后我决定清理错误处理(在阅读 this 之后),通过引入带有一些异常处理程序方法(被正确调用)的全局 @ControllerAdvice 注释类,但在某处响应被转换为 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() {}
}

【问题讨论】:

  • 您希望响应是 json 响应吗?
  • 是的,当我成功通话时,我希望得到 json (application/json)。特别是作为 json 的 RestErrorInfo 对象。
  • 检查我的答案。它与您的代码相同,但带有 @ResponseBody 注释(在您包含的教程中使用)

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


【解决方案1】:

将代码改成如下:

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

【讨论】:

  • 非常感谢!我已经坐了几个小时了,问题是我什至无法正确复制/粘贴:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-25
相关资源
最近更新 更多