gpt4 book ai didi

java - 某些东西(Tomcat?)取代了我的 Rest 响应

转载 作者:行者123 更新时间:2023-11-28 22:37:40 24 4
gpt4 key购买 nike

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

然后我决定清理错误处理(在阅读 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 对象。

我正在使用: Spring 4.0.3.RELEASE Spring 启动 1.0.1.RELEASE Spring 安全 3.2.1.RELEASE

还有一件事可能会搞砸:我使用过滤器在 header 中设置 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() {}
}

最佳答案

将代码更改为以下内容:

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

关于java - 某些东西(Tomcat?)取代了我的 Rest 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23108832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com