使用 JAX-RS 应用程序未在 Apache Wink Rest 客户端错误上接收正文

Not receiving body on Apache Wink Rest Client error with JAX-RS Application

我需要在我的 JAX-RS 应用程序中对外部源进行 JSON API 调用。根据我的研究,为了做到这一点,我需要使用有效的 RestClient()。但只有当我得到成功的回应时。当状态代码不是 400 时,它无法解析正文。这是我得到 200 status:

时的工作代码示例
Resource resource = client.resource("https://api.paytrace.com/v1/customer/create");
String response = resource
    .contentType(MediaType.APPLICATION_JSON)
    .header("Authorization", "Bearer " + accessToken)
    .accept(MediaType.APPLICATION_JSON)
    .post(String.class, paytraceJSON);

我目前正尝试注销 response,但当响应详细说明正文中出现错误时,我什么也没得到。有没有更好的方法来处理这个问题,这样我就可以从我自己使用的服务 API 中回显错误?

而不是使用 Resource 来处理所有事情,我能够利用转换为 ClientResponse 而不是 String 这将 return 我返回错误而不仅仅是文本当响应失败时(无论出于何种原因)。

我需要在我的 JAX-RS 应用程序中对外部源进行 JSON API 调用。根据我的研究,为了做到这一点,我需要使用有效的 RestClient()。但只有当我得到成功的回应时。当状态代码不是 400 时,它无法解析正文。这是我获得 200 状态时的工作代码示例:

旧方法:

Resource resource = client.resource("https://api.com");
String response = resource
    .contentType(MediaType.APPLICATION_JSON)
    .header("Authorization", "Bearer " + accessToken)
    .accept(MediaType.APPLICATION_JSON)
    .post(String.class, payload);
//Parse the String here to turn it into a JSONObject

新方式:

Resource resource = client.resource("https://api.com/");
ClientResponse response = resource.contentType("application/json")
        .accept("application/json")
        .header("Authorization", "Bearer " + accessToken)
        .post(payload);
JSONObject customerResponseObject = response.getEntity(JSONObject.class);