json - How automatically parse response String into Map using RestTemplate -
i'm using resttemplate retrieve list of issues jira. response string lots of fields, of them arrays. request looks like:
responseentity<string> response = resttemplate.exchange(url, httpmethod.get, entity, string.class); response string looks like:
{ "expand": "schema,names", "total": 12, "issues": [ { "id": "32", "key": "test-1", "fields": { "fixversions": [ { "description": "", "releasedate": "2017-04-02" } ] }, { "id": "32", "key": "test-2", "fields": { "fixversions": [ { "description": "", "releasedate": "2017-04-01" } ] } ] } is possible convert string map, object string or list of map or this, without defining appropriate objects. result, i'd have possibility access description by: response.getissues().get(0).getfields().getfixversion().get(0).getdescription()
in such occasion, defining chain of specific objects looks cumbersome.
you can create own pojo classes corresponds structure of response json. based on json have shared, can have class structure :
public class response { private string expand; private string total; private list<issues> issues; } public class issues { private string id; private string key; private map<string, list<fixversions> fields; } public class fixversions { private string description; private string releasedata; } your get call change following :
responseentity response = resttemplate.exchange(url, httpmethod.get, entity, response.class);
p.s. - fields in pojo class must have getters , setters well.
Comments
Post a Comment