我试图强制 Resteasy 客户端抛出从响应中的状态代码和实体映射的我自己的/自定义的业务异常。
我知道如何将我自己的状态代码的异常映射到服务器端 exception mappers ,但我不知道如何在客户端自动“取消映射”它们(当然使用一些“取消映射”逻辑)以获得正确的异常...
我知道客户端的异常映射是在 ClientInvocation#handleErrorStatus 中完成的.
问题是我怎样才能覆盖这个默认行为?!
最佳答案
您可以使用 ClientResponseFilter 拦截 ClientInitation :
@Provider
public class ClientErrorFilter implements ClientResponseFilter {
@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
if (responseContext.getStatus() == 404) {
throw new CustomNotFoundException();
}
}
}
缺点是您的异常将被包装在 ResponseProcessingException
中:
Client client = ClientBuilder.newClient().register(ClientErrorFilter.class);
try {
client.target("http://stackoverflow.com/something-not-found").request().get();
} catch (ResponseProcessingException ex) {
CustomNotFoundException nfex = (CustomNotFoundException) ex.getCause();
}
关于java - 客户端异常映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25241039/