【问题标题】:how to get the result of restful web service with httpClient in json format如何使用 json 格式的 httpClient 获取 restful web 服务的结果
【发布时间】:2015-04-09 23:10:07
【问题描述】:

我正在开发一个 httpclient 应用程序,它使用来自 JSON 格式的 RESTful Web 服务的list<OBJECT>

我想以 JSON 格式使用此结果。

这是我的 httpclient 应用程序类:

public class ClientAbortMethod {

    public final static void main(String[] args) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpGet httpget = new HttpGet("http://localhost:8080/structure/alldto");

            System.out.println("Executing request " + httpget.getURI());
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {

              String data= EntityUtils.toString(response.getEntity());


                System.out.println(data);
                // Do not feel like reading the response body
                // Call abort on the request object
                httpget.abort();
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }

}

这是执行这段代码的结果

[{"ids":"#AB","champs":[ {"idChamp":1,"order":1,"type":"FROM"},{"idChamp":2,"order":2,"type":"TO"},{"idChamp":3,"order":3,"type":"TEXT"}]},{"ids":"#AC","champs":[{"idChamp":4,"order":1,"type":"FROM"},{"idChamp":5,"order":2,"type":"TO_MAIL"},{"idChamp":6,"order":3,"type":"TEXT"}]},{"ids":"tt","champs":[]}]

这是宁静的网络服务

@RequestMapping("/alldto")
public List<StructureNotificationDto> GetALLStructureNotification() {
     return StructureNotif.StructureDTOS();
}

我怎样才能获得 JSON 格式或其他易于操作的格式的 Web 服务的结果?

【问题讨论】:

  • 我不明白。 data 包含响应内容为String,响应内容为 JSON。你还想要什么? JSON 只是文本。
  • 数据以String格式表示Web服务的结果,但我想使用JSON或List格式的Web服务结果,
  • 因为 web 服务返回一个列表

标签: java arrays json rest httpclient


【解决方案1】:

您可以使用Jackson 库。要添加库,我建议使用依赖管理系统,例如 Maven。之后,使用以下代码:

   ObjectMapper mapper = new ObjectMapper();
   List<StructureNotificationDto> list = mapper.readValue(data, new TypeReference<List<StructureNotificationDto>>() {});

然后你就可以操作数据了。

【讨论】:

  • 添加杰克逊并使用此代码后,我收到此错误:
  • 线程“main”中的异常 org.codehaus.jackson.map.JsonMappingException:没有找到适合类型 [简单类型,类 com.onlineproject.domain.StructureNotificationDto] 的构造函数:无法从 JSON 对象实例化(需要添加/启用类型信息?)在 [Source: java.io.StringReader@5427c60c;行:1,列:3]
  • 在添加 Jackson 并使用此代码后,我收到此错误:线程“main”中的异常 org.codehaus.jackson.map.JsonMappingException:没有找到适合类型 [simple type, class com. onlineproject.domain.StructureNotificationDto]:无法从 JSON 对象实例化(需要添加/启用类型信息?)在 [Source: java.io.StringReader@5427c60c;行:1,列:3]
  • 如果您没有最终字段,只需确保您有所有方法的 getter/setter 和不带参数的构造函数(将构造函数设为私有,这样其他人就不能使用它)。如果您有最终字段,请在您的私有构造函数中为它们分配虚拟值。杰克逊会做剩下的事情。
【解决方案2】:

也许您想将 JSON 字符串转换为 Java 对象,以便轻松操作它。为此,您可以使用org.json

请参考this问题。

【讨论】:

    【解决方案3】:

    您想使用 Jackson、GSON 手动将 JSON 响应字符串转换为 Java 对象。 这是使用GSONJackson将JSON字符串转换为Java对象的示例

    【讨论】:

      【解决方案4】:
      ClientResponse response = resourse.type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
      if(response.getStatus() == 200)
          return response.getEntity(String.class);
      ObjectMapper mapper = new ObjectMapper();
      ConfToggleBO togleObj = mapper.readValue(enitity, ConfToggleBO.class);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-17
        • 2014-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多