mar*_*31b 7 java jax-rs jersey jersey-2.0 spring-boot
我遇到了与这个问题相同的问题,使用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();
}
}
Run Code Online (Sandbox Code Playgroud)
我的代码返回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
Run Code Online (Sandbox Code Playgroud)
客户方(卷曲):
$ 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
Run Code Online (Sandbox Code Playgroud)
所以它总是404.除非我确实有这样的/错误资源,那么呢?我应该回来的是什么?我当时所有的都是对/错误的GET请求.而且我不希望那些额外的请求消耗资源并污染我的日志.
我错过了什么?如果没有,我的异常处理应该怎么办?
您可以将 Jersey 属性设置ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR为true。
每当响应状态是
4xx或者5xx可以在容器特定的响应实现之间进行选择sendError时setStatus。例如,在 Servlet 容器 Jersey 上可以调用HttpServletResponse.setStatus(...)或HttpServletResponse.sendError(...)。调用
sendError(...)方法通常会重置实体、响应标头并为指定的状态代码(例如servleterror-page配置)提供错误页面。但是,如果您想对响应进行后处理(例如通过 servlet 过滤器),唯一的方法是调用setStatus(...)容器 Response 对象。如果属性值为 true,
Response.setStatus(...)则使用该方法而不是 defaultResponse.sendError(...)。属性值的类型是
boolean。默认值为false。
property(key, value)您只需调用子类构造函数即可设置 Jersey 属性ResourceConfig。
| 归档时间: |
|
| 查看次数: |
2826 次 |
| 最近记录: |