java - how to get the result of restful web service with httpClient in json format -
i'm developing httpclient application use list<object>
restful web service in json format.
i want use result in json format.
this httpclient application class :
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); // not feel reading response body // call abort on request object httpget.abort(); } { response.close(); } } { httpclient.close(); } } }
this result of executing code
[{"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":[]}]
this restful web service
@requestmapping("/alldto") public list<structurenotificationdto> getallstructurenotification() { return structurenotif.structuredtos(); }
how can result of web service in json format or other easy-to-manipulate format?
you can use jackson library. add libraries, suggest dependency management system, maven. after that, use following code:
objectmapper mapper = new objectmapper(); list<structurenotificationdto> list = mapper.readvalue(data, new typereference<list<structurenotificationdto>>() {});
then can manipulate data.