[英]java jackson: get from url rest web service call pojo's (with inner pojo's) as json and map them back to pojo
我正在使用傑克遜,情況是:
盡管我四處搜尋並找出以下起點,但我不知道如何完成此代碼以使事情正常進行,但是后來我完全堆砌了,需要幫助...
“復雜的pojo”:
public class MyPojo { private List<MyOtherPojo> myOtherPojo; private List<HashMap<String, String>> listOfMaps; private Map<String, String> map; //Constructors, getters & setters }
客戶端代碼:
String wsURI = "server.com/myservice?param1=a¶m2=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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.