Java 如何处理弹簧靴';s重定向到/错误?

Java 如何处理弹簧靴';s重定向到/错误?,java,spring-boot,jersey,jax-rs,jersey-2.0,Java,Spring Boot,Jersey,Jax Rs,Jersey 2.0,我遇到了与中相同的问题,使用Spring Boot 1.3.0并且没有使用@RestController注释我的控制器,只是@Path和@Service。正如问题中的OP所说 对我来说,这一点都不明智 我也不明白为什么他们会让它重定向到/error。很可能我遗漏了什么,因为我只能给客户404或200秒 我的问题是他的解决方案似乎不适用于1.3.0,因此我有以下请求流:假设我的代码抛出一个NullPointerException。它将由我的exceptionappers之一处理 @Provider

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

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

我也不明白为什么他们会让它重定向到/error。很可能我遗漏了什么,因为我只能给客户404或200秒

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

@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();
    }
}
@Provider
公共类GeneralExceptionMapper实现ExceptionMapper{
私有静态最终记录器Logger=LoggerFactory.getLogger(GeneralExceptionMapper.class);
@凌驾
公众响应(可丢弃的例外){
LOGGER.error(exception.getLocalizedMessage());
返回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
2015-12-16 18:33:21.268信息9708---[nio-8080-exec-1]o.glassfish.jersey.filter.LoggingFilter:1*服务器已在线程http-nio-8080-exec-1上收到请求
1>获取http://localhost:8080/nullpointerexception
1>接受:*/*
1>主机:本地主机:8080
1>用户代理:curl/7.45.0
2015-12-16 18:33:29.492 INFO 9708---[nio-8080-exec-1]o.glassfish.jersey.filter.LoggingFilter:1*服务器在线程http-nio-8080-exec-1上响应
1 < 500
2015-12-16 18:33:29.540信息9708---[nio-8080-exec-1]o.glassfish.jersey.filter.LoggingFilter:2*服务器已在线程http-nio-8080-exec-1上收到请求
2>获得http://localhost:8080/error
2>接受:*/*
2>主机:本地主机:8080
2>用户代理:curl/7.45.0
2015-12-16 18:33:37.249 INFO 9708---[nio-8080-exec-1]o.glassfish.jersey.filter.LoggingFilter:2*服务器在线程http-nio-8080-exec-1上响应
2 < 404
和客户端(卷曲):

$curl-vhttp://localhost:8080/nullpointerexception
*状态:INIT=>connecthandle0x6000572d0;第1090行(连接#-5000)
*已添加连接0。缓存现在包含1个成员
*正在尝试::1。。。
*状态:CONNECT=>WAITCONNECT句柄0x6000572d0;第1143行(连接#0)
*已连接到本地主机(::1)端口8080(#0)
*状态:WAITCONNECT=>SENDPROTOCONNECT句柄0x6000572d0;第1240行(连接#0)
*状态:SENDPROTOCONNECT=>DO句柄0x6000572d0;第1258行(连接#0)
>GET/nullpointerexception HTTP/1.1
>主机:本地主机:8080
>用户代理:curl/7.45.0
>接受:*/*
>
*状态:DO=>DO_DONE句柄0x6000572d0;第1337行(连接#0)
*状态:DO_DONE=>WAITPERFORM句柄0x6000572d0;第1464行(连接#0)
*状态:WAITPERFORM=>performhandle0x6000572d0;第1474行(连接#0)
*HTTP 1.1或更高版本,支持持久连接和流水线
未找到DONE句柄0x6000572d0;第1632行(连接#0)
*卷好了
*与主机localhost的连接#0保持不变
所以它总是一个404。除非我有这样的错误资源,那又怎样?我应该还什么?在这一点上,我只有一个GET请求/错误。我不希望这些额外的请求消耗资源并污染我的日志


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

您可以将Jersey属性设置为
true

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

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

如果属性值为true,则方法
Response.setStatus(…)
将用于默认的
Response.senderro(…)

属性值的类型为
布尔值
。默认值为
false

只需在
ResourceConfig
子类构造函数中调用
property(key,value)
即可设置Jersey属性

$ 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