我不明白为什么当我收到 json 编码的字符串时,它不会自动解码。 我使用 Apache CFX 3.1.4 或 Jersey 2.22.1 尝试了此代码:
Client client = ClientBuilder.newClient().register(JSONProvider.class);
WebTarget target = client.target("http://example.org/rest/service1");
target = target.queryParam("method", "method1");
Entity<EndpointRequest> entity = Entity.entity(new EndpointRequest("0000"),
MediaType.APPLICATION_JSON);
Response response = target.request()
.accept(MediaType.APPLICATION_JSON)
.post(entity);
System.out.println( response.getStatus() );
if (response.getStatus() == 200) {
// The problem comes here
String basePath = response.readEntity(String.class);
System.out.println( basePath );
}
请求已成功执行,但basePath
包含“\/opt\/local\/application\/rest\/”
(包括反斜杠和双引号)
basePath
应包含以下内容:/opt/local/application/rest/
在我看来,json 反序列化在应该触发的时候却没有被触发。
最佳答案
您创建了一个 Client 和一个 WebTarget 但从未实际使用过它们。您最终会使用实体和响应。我不确定您使用的是什么库,但我认为您需要实际使用 Ciient 和 WebTarget 来调用 JSONProvider。
关于java - 使用 Java REST 客户端(Apache CXF 或 Jersey)读取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34513445/