如何处理Spring Boot重定向到/error的问题?

7
我遇到了与此问题相同的问题,使用Spring Boot 1.3.0并没有在我的控制器中注释@RestController,只有@Path@Service。正如那个问题的OP所说:

对我来说,这一点都不明智

我也不明白为什么他们要将其重定向到/error。而且很可能是我漏掉了什么,因为我只能向客户端返回404或200。
我的问题是他的解决方案似乎在1.3.0上不起作用,所以我的请求流程如下:假设我的代码抛出NullPointerException。它会被我的一个ExceptionMapper处理。
@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错误,但是它试图将其重定向到/error,而不是将其发送回客户端。如果我没有其他资源可以提供,那么就会返回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。除非我有这样的“/error”资源,那么该怎么办?我应该返回什么?那时我只有对“/error”的GET请求。我不想让这些额外的请求消耗资源并污染我的日志。

我错过了什么?如果没有遗漏,那么我该如何处理异常?

1个回答

4
您可以将Jersey属性ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR设置为true
无论响应状态是4xx还是5xx,都可以在容器特定的响应实现中选择sendErrorsetStatus。例如,在servlet容器上,Jersey可以调用HttpServletResponse.setStatus(...)HttpServletResponse.sendError(...)
调用sendError(...)方法通常会重置实体、响应标头并为指定的状态代码提供错误页面(例如servlet error-page配置)。但是,如果您想对响应进行后处理(例如通过servlet过滤器),唯一的方法是调用容器响应对象上的setStatus(...)
如果属性值为true,则使用Response.setStatus(...)方法而不是默认的Response.sendError(...)
属性值的类型为boolean。默认值为false
您可以通过在ResourceConfig子类构造函数中调用property(key, value)来简单地设置Jersey属性。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接