如何处理Spring Boot重定向到/ error? [英] How to handle Spring Boot's redirection to /error?

查看:1669
本文介绍了如何处理Spring Boot重定向到/ error?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了与这个问题相同的问题,使用了Spring Boot 1.3.0并没有用 @RestController 注释我的控制器,只是 @Path @服务。正如该问题中的OP所说,

I've encountered the same issue as in this question, using Spring Boot 1.3.0 and not having my controllers annotated with @RestController, just @Path and @Service. As the OP in that question says,


这对我来说,除了明智之外什么

this is, to me, anything but sensible

我也无法理解他们为什么会将其重定向到/ error。而很可能是我遗漏了某些东西,因为我只能向客户回馈404或200.

I also can't understand why would they have it redirect to /error. And it is very likely that I'm missing something, because I can only give back 404s or 200s to the client.

我的问题是他的解决方案似乎不适用于1.3.0,所以我有以下请求流程:假设我的代码抛出 NullPointerException 。它将由我的一个 ExceptionMapper s

My problem is that his solution doesn't seem to work with 1.3.0, so I have the following request flow: let's say my code throws a NullPointerException. It'll be handled by one of my ExceptionMappers

@Provider
public class GeneralExceptionMapper implements ExceptionMapper<Throwable> {

    private static final Logger LOGGER = LoggerFactory.getLogger(GeneralExceptionMapper.class);

    @Override
    public Response toResponse(Throwable exception) {
        LOGGER.error(exception.getLocalizedMessage());
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
}

我的代码返回500,但不是发送它回到客户端,它试图将其重定向到/错误。如果我没有其他资源,它将发回404。

And my code returns a 500, but instead of sending it back to the client, it tries to redirect it to /error. If I don't have another resource for that, it'll send back a 404.

2015-12-16 18:33:21.268  INFO 9708 --- [nio-8080-exec-1] o.glassfish.jersey.filter.LoggingFilter  : 1 * Server has received a request on thread http-nio-8080-exec-1
1 > GET http://localhost:8080/nullpointerexception
1 > accept: */*
1 > host: localhost:8080
1 > user-agent: curl/7.45.0

2015-12-16 18:33:29.492  INFO 9708 --- [nio-8080-exec-1] o.glassfish.jersey.filter.LoggingFilter  : 1 * Server responded with a response on thread http-nio-8080-exec-1
1 < 500

2015-12-16 18:33:29.540  INFO 9708 --- [nio-8080-exec-1] o.glassfish.jersey.filter.LoggingFilter  : 2 * Server has received a request on thread http-nio-8080-exec-1
2 > GET http://localhost:8080/error
2 > accept: */*
2 > host: localhost:8080
2 > user-agent: curl/7.45.0

2015-12-16 18:33:37.249  INFO 9708 --- [nio-8080-exec-1] o.glassfish.jersey.filter.LoggingFilter  : 2 * Server responded with a response on thread http-nio-8080-exec-1
2 < 404

客户方(curl):

$ curl -v http://localhost:8080/nullpointerexception
* STATE: INIT => CONNECT handle 0x6000572d0; line 1090 (connection #-5000)
* Added connection 0. The cache now contains 1 members
*   Trying ::1...
* STATE: CONNECT => WAITCONNECT handle 0x6000572d0; line 1143 (connection #0)
* Connected to localhost (::1) port 8080 (#0)
* STATE: WAITCONNECT => SENDPROTOCONNECT handle 0x6000572d0; line 1240 (connection #0)
* STATE: SENDPROTOCONNECT => DO handle 0x6000572d0; line 1258 (connection #0)
> GET /nullpointerexception HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.45.0
> Accept: */*
>
* STATE: DO => DO_DONE handle 0x6000572d0; line 1337 (connection #0)
* STATE: DO_DONE => WAITPERFORM handle 0x6000572d0; line 1464 (connection #0)
* STATE: WAITPERFORM => PERFORM handle 0x6000572d0; line 1474 (connection #0)
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 404 Not Found
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Content-Length: 0
< Date: Wed, 16 Dec 2015 17:33:37 GMT
<
* STATE: PERFORM => DONE handle 0x6000572d0; line 1632 (connection #0)
* Curl_done
* Connection #0 to host localhost left intact

所以它总是404.除非我确实有这样的/错误资源,那么什么?我应该回来的是什么?我当时所有的都是对/错误的GET请求。而且我不希望这些额外的请求消耗资源并污染我的日志。

So it's always a 404. Unless I do have such an /error resource, then what? what am I supposed to return? All I have at that point is a GET request to /error. And I don't want those extra requests consuming resources and polluting my logs.

我缺少什么?如果没有,我的异常处理应该怎么做?

What am I missing? And if nothing, what should I do with my exception handling?

推荐答案

你可以设置Jersey属性 ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR true

You can set the Jersey property ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR to true.


每当响应状态为<$ c时$ c> 4xx 或 5xx 可以在 sendError 或<$ c之间进行选择$ c> setStatus 关于特定于容器的响应实现。例如。在servlet容器上,Jersey可以调用 HttpServletResponse.setStatus(...) HttpServletResponse.sendError(...)

Whenever response status is 4xx or 5xx it is possible to choose between sendError or setStatus on container specific Response implementation. E.g. on servlet container Jersey can call HttpServletResponse.setStatus(...) or HttpServletResponse.sendError(...).

调用 sendError(...)方法通常会重置实体,响应标头并为指定的状态代码提供错误页面(例如servlet error-page 配置)。但是,如果要进行后处理响应(例如通过servlet过滤器),唯一的方法是在容器响应对象上调用 setStatus(...)

Calling sendError(...) method usually resets entity, response headers and provide error page for specified status code (e.g. servlet error-page configuration). However if you want to post-process response (e.g. by servlet filter) the only way to do it is calling setStatus(...) on container Response object.

如果属性值为true,则使用方法 Response.setStatus(...)而不是默认值 Response.sendError(...)

If property value is true the method Response.setStatus(...) is used over default Response.sendError(...).

属性值的类型是 boolean 。默认值为 false

Type of the property value is boolean. The default value is false.

您可以通过调用属性(键,值)来设置Jersey属性 ResourceConfig 子类构造函数中。

You can set Jersey property simply by calling property(key, value) in your ResourceConfig subclass constructor.

这篇关于如何处理Spring Boot重定向到/ error?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆