spring - RestTemplate + Jackson -


i want use spring's resttemplate plus jackson consume webservice. have followed several tutorials , have come point of creating daos. method of domain objects:

// create rest template resttemplate resttemplate = new resttemplate();  // create list message converters  list<httpmessageconverter<?>> messageconverters = new arraylist<httpmessageconverter<?>>();  // add jackson message converter messageconverters.add(new mappingjacksonhttpmessageconverter());  // add message converters resttemplate resttemplate.setmessageconverters(messageconverters);  list<station> resultlist = arrays.aslist(resttemplate.getforobject(base_url, station[].class));  return resultlist; 

but web service not return array of station objects right away, rather more semantic expression in way:

{"success":true,"message":"records retrieved successfully","data":{"totalcount":"14","stations":[{"id":"1264","station":"station 1","idjefatura":"1","syncdate":"2013-01-24 13:20:43"}, ...] }} 

so problem is, i'm not sure how "tell" resttemplate parse object list right after "stations" indicator, without creating ad hoc object, not seem proper solution.

is there way specify right syntax resttemplate?

edit: created wrapper object this:

public class restresponseobject {      private boolean success;     private string message;     private data data;      public data getdata() {         return data;     }      public void setdata(data data) {         this.data = data;     }      public boolean issuccess() {         return success;     }      public void setsuccess(boolean success) {         this.success = success;     }      public string getmessage() {         return message;     }      public void setmessage(string message) {         this.message = message;     }      public class data {         private int totalcount;         private list<station> stations;          public int gettotalcount() {             return totalcount;         }          public void settotalcount(int totalcount) {             this.totalcount = totalcount;         }          public list<station> getstations() {             return stations;         }          public void setstations(list<station> estaciones) {             this.stations= estaciones;         }     } } 

but struggling how make object generic, since key name of object list in json response dependant of domain object's class.

there 2 solutions here:

  1. you can write own deserializer implementation, parse json , take station list , convert list object. deserializer can set on resttemplate. have @ how write custom desrializer jackson
  2. the other thing can write class maps rest response. class should contain list object member variable. spring default convert new class , can stations class.

here example.

the response class

public class myresponseclass {       // other variables      private list<station> stations; //it getters , setters } 

in rest client

myresponseclass response = resttemplate.getforobject(base_url, myresponseclass.class) list<station> resultlist = response.getstations() 

Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -