gpt4 book ai didi

java jackson : get from url rest web service call pojo's (with inner pojo's) as json and map them back to pojo

转载 作者:行者123 更新时间:2023-11-30 03:19:20 26 4
gpt4 key购买 nike

我使用的是jackson,场景是:

  1. 在客户端 (Android) 中生成对 Web 服务的 URL 调用
  2. Web 服务以 json 形式返回“complex pojo”(具有与其他 pojo 相同的类成员)
  3. 返回客户端将 json 映射到 pojo

我不知道如何完成这段代码以使事情正常工作,尽管我进行了全面搜索并找出了以下起点,但后来我完全堆积起来了,我需要帮助......

  1. “复杂的 pojo”:

    public class MyPojo {
    private List<MyOtherPojo> myOtherPojo;
    private List<HashMap<String, String>> listOfMaps;
    private Map<String, String> map;
    //Constructors, getters & setters
    }
  2. 客户端代码:

    String wsURI = "server.com/myservice?param1=a&param2=b";
    try {
    URL url = new URL(wsURI);
    URLConnection connection = url.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    StringBuilder sbuilder = new StringBuilder();
    String aux = "";
    while ( (aux = in.readLine()) != null) {
    sbuilder.append(aux);
    }
    ObjectMapper mapper = new ObjectMapper();
    //TODO get myPojo object from his representation as string at sbuilder.toString();
    myPojo = ???;
    } catch (Exception e) {}

最佳答案

解决方案感谢@peeskillet:

MyPojo myPojo = mapper.readValue(connection.getInputStream(), MyPojo.class);

关于java jackson : get from url rest web service call pojo's (with inner pojo's) as json and map them back to pojo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31722251/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com