我有一个 Jersey 客户端,它调用一个 api,其返回类型为 javax.ws.rs.core.Response(如所使用的接口(interface)中所定义)。
public Response getResponse(String id)
调用的响应状态为“服务器错误”,但客户端不会抛出InternalServerException,而是返回InboundJaxrsResponse。
在检查 Jersey 代码时,我发现 JerseyInvocation所使用的类具有检查响应类型是否为 javax.ws.rs.core.Response 然后返回 InboundJaxrsResponse 对象的逻辑。 如何根据此处的响应状态获得适当的异常?
PS:RestEasy客户端也有类似的逻辑。
最佳答案
假设您使用 <T> T get(Class<T> responseType)
要从 HTTP 响应正文中获取整数,如果响应状态不是 2xx,则可能抛出的异常是 WebApplicationException
。
就您的情况而言,未满足 API 契约(Contract)的第二个要求
WebApplicationException
- in case the response status code of the response returned by the server is not successful and the specified response type is notResponse
.
关于java - JerseyClient 不会针对 4xx 和 5xx 响应引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49302059/