繁体   English   中英

无法从Jersey GET响应获取JSON数据

[英]Cannot get JSON data from Jersey GET response

我正在开发一个宁静的Web客户端,并尝试从GET方法的响应中获取JSON有效负载。 我正在使用泽西岛。 但是我只是无法使用response.getEntity()方法读取JSON数据。 我尝试了许多方法,包括response.bufferEntity(),但输出始终保持为空。 以下是我的代码和输出,此外,我还可以在Wireshark中捕获的响应数据包中看到JSON数据。 我非常感谢每个尝试帮助找出原因或提供解决方案的人。 谢谢!

码:

    public JSONObject Get(String requestPath){

    ClientResponse response = webResource.path(requestPath)
            .header("Content-Type", contTypeHeader )
            .header("Accept",acceptHeader)
            .header("Authorization", authZ )
            .get(ClientResponse.class);

    response.bufferEntity();
    if (!(response.getStatus() == 201 || response.getStatus() == 200)) {
        throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
    }

    System.out.println(response.getEntity(JSONObject.class));
    return null;

}

输出总是像这样:{}

除非您有MessageBodyReader ,否则您不能使用JSONObject JAX-RS实体提供者中查看更多内容。 您当前正在使用的提供程序(可能是Jackson)仅支持JavaBean POJO或它们的集合。 例如,如果您有此JSON

{ "property1" : "value1", "property2": "value2" }

然后,您需要像这样的POJO

public class Bean {
    private String property1;
    private String property2;
    // getters and setters
}

然后,您可以执行getEntity(Bean.class) 之所以得到一个空对象,是因为反序列化可以解决设置程序的问题。 它在JSON上查找与设置器匹配的属性,并使用它来设置属性。 JSON对象你的JSON属性没有setter方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM