RestEasy从拦截器引发的异常永远不会调用ExceptionMapper

时间:2019-02-11 09:45:25

标签: jax-rs resteasy

我有一个资源,它可以通过RestEasy调用另一个REST端点

如果此调用失败(即响应状态代码不是2xx),我想返回自定义错误响应。

我现在拥有的是一个拦截器:

@Provider
public class AdapterInterceptor implements ContainerResponseFilter {

    @Override
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
        if (responseContext.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
            log.error("Error calling adapter. Response Status '{}' Response Body '{}'", responseContext.getStatus(), responseContext.getEntity());
            throw new AdapterException(responseContext);
        }
    }
}

和一个ExceptionMapper

@Provider
public class AdapterExceptionMapper implements ExceptionMapper<AdapterException> {

    @Override
    public Response toResponse(AdapterException exception) {
        return Response
                .status(Response.Status.BAD_REQUEST)
                .entity("custom error response")
                .build();
    }
}

我的资源:

@Path("/test")
public class FrontendResource {
    private SchemeAdapterService schemeAdapterService;

    public FrontendResource() throws URISyntaxException {
        ResteasyClient client = new ResteasyClientBuilder().build();
        ResteasyWebTarget target = client.target(new URI("http://localhost:8080/mock-adapter"));
        schemeAdapterService = target.proxy(SchemeAdapterService.class);
    }

    @Path("/hello")
    @POST
    public Response test(@Valid Data data) {
        PreCheckinResponse preCheckinResponse = schemeAdapterService.callTest(data);
        return Response.ok(preCheckinResponse).build();
    }
}

拦截器已正确触发,并引发AdapterException。但是,AdapterException(RuntimeException)永远不会在AdapterExceptionMapper中映射,而我最终得到一个org.jboss.resteasy.spi.UnhandledException

如果我直接从资源抛出异常,则将正确触发ExceptionMapper。从拦截器看来,它似乎不起作用。我该如何解决这个问题?

0 个答案:

没有答案