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

我遇到了与这个问题相同的问题 ,使用了Spring Boot" title="Spring Boot">Spring Boot1.3.0,而我的控制器没有用@RestController,just @Path@Service。正如该问题中的操作人员所说,

对我来说,这不是明智的选择

我也无法理解为什么他们会将其重定向到/ error。而且 ,因为我只能将404或200s退还给客户端。

我的问题是他的解决方案似乎不适用于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请求。而且我不希望那些额外的请求消耗资源并污染我的日志。

我想念什么?如果没有,我应该如何处理异常?

回答:

您可以将Jersey属性设置ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERRORtrue

每当响应状态为4xx5xx有可能之间进行选择sendErrorsetStatus在容器具体响应实现。例如,在servlet容器Jersey上可以调用HttpServletResponse.setStatus(...)HttpServletResponse.sendError(...)

调用sendError(...)方法通常会重置实体,响应标头,并为指定的状态代码(例如Servlet error-

page配置)提供错误页面。但是,如果要对响应进行后处理(例如,通过servlet过滤器),则唯一的方法是调用setStatus(...)容器Response对象。

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

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

你可以简单地通过调用设置泽西财产property(key, value)在你的ResourceConfig子类的构造。

以上是 如何处理Spring Boot的/ error重定向? 的全部内容, 来源链接: utcz.com/qa/433066.html

回到顶部