繁体   English   中英

使用Java REST Client(Apache CXF或Jersey)读取字符串

[英]Reading a string with Java REST Client (Apache CXF or Jersey)

我不明白为什么当我收到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反序列化尚未触发。

您创建了一个客户端和一个WebTarget,但从未实际使用它们。 您最终改为使用实体和响应。 我不确定您使用的是哪个库,但我认为您实际上需要使用Ciient和WebTarget来调用JSONProvider。

问题是服务器响应。 即使正确编码,服务器也不应仅返回带引号的字符串,因为字符串不是有效的JSON对象

暂无
暂无

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

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