Java JAX-RS拦截器映射void方法上的异常

Java JAX-RS拦截器映射void方法上的异常,java,jax-rs,resteasy,void,Java,Jax Rs,Resteasy,Void,我试图使用拦截器来内省和更改后端发生的异常。拦截器如下所示: public class ApplicationErrorInterceptor { private static final Logger LOGGER = Logger.getLogger(ApplicationErrorInterceptor.class); @AroundInvoke public Object handleException(InvocationContext context) {

我试图使用拦截器来内省和更改后端发生的异常。拦截器如下所示:

public class ApplicationErrorInterceptor {
    private static final Logger LOGGER = Logger.getLogger(ApplicationErrorInterceptor.class);

    @AroundInvoke
    public Object handleException(InvocationContext context) {
        try {
            return context.proceed();
        } catch (Exception ex) {
            LOGGER.errorf(ex, "An unhandled exception occured!");
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
                    .entity("some custom 500 error text")
                    .build();
        }
    }
}
@Path("/somepath")
@Interceptors({ApplicationErrorInterceptor.class})
public interface SomeRestService {
    @POST
    @Path("getResponse")
    Response getResponse();

    @POST
    @Path("getVoid")
    void getVoid();
}
使用此拦截器的服务如下所示:

public class ApplicationErrorInterceptor {
    private static final Logger LOGGER = Logger.getLogger(ApplicationErrorInterceptor.class);

    @AroundInvoke
    public Object handleException(InvocationContext context) {
        try {
            return context.proceed();
        } catch (Exception ex) {
            LOGGER.errorf(ex, "An unhandled exception occured!");
            return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
                    .entity("some custom 500 error text")
                    .build();
        }
    }
}
@Path("/somepath")
@Interceptors({ApplicationErrorInterceptor.class})
public interface SomeRestService {
    @POST
    @Path("getResponse")
    Response getResponse();

    @POST
    @Path("getVoid")
    void getVoid();
}

假设这两个方法的实现都会引发异常。我希望在这两种情况下,异常都会映射到500服务器错误,并带有提供的自定义消息。不幸的是,返回void的方法将被映射到204 No Content响应。如果我完全删除拦截器,则会出现默认的500服务器错误,这至少是正确的状态代码,但我丢失了错误自定义

您认为在ExceptionMapper中有什么问题吗?我还没有尝试异常映射程序,没有。虽然对于给定的示例来说,它可能已经足够了,但它不允许访问原始响应,这是我通常希望能够看到的东西。这个拦截器看起来像Spring AOP。我认为您将无法自定义void方法,因为您正在拦截该方法的结果,而它返回void not Response。记住ExceptionMapper。你是对的,这种拦截器太早了,只会吃掉异常。忽略返回值是有意义的,尽管一开始有点难以注意和调试。我必须使用异常映射器或JAX-rsf提供的其他机制。您应该只返回void方法的响应,以保持其一致性。