How to consume JSON response with spring restemplate properly? -
i have spring mvc rest service return json value, have 8 row, here json
[ {"no":"1","date":"2015-03-30","grandtotal":699618,"diskon":699618}, {"no":"2","date":"2015-03-30","grandtotal":1867949,"diskon":1867949}, {"no":"3","date":"2015-03-27","grandtotal":2190909,"diskon":2190909}, {"no":"4","date":"2015-03-26","grandtotal":8616120,"diskon":8616120}, {"no":"5","date":"2015-03-26","grandtotal":1095455,"diskon":1095455}, {"no":"6","date":"2015-03-24","grandtotal":938961,"diskon":938961}, {"no":"7","date":"2015-03-24","grandtotal":5603848,"diskon":5603848}, {"no":"8","date":"2015-03-20","grandtotal":3735899,"diskon":3735899} ]
what trying.. here controller. springrestcontroller.java jackson way :
@requestmapping(value = "/view", method = requestmethod.get) public string initcreationform(map<string, object> model) { string url = "http://localhost:8080/springservicejson/view/"; resttemplate resttemplate = new resttemplate(); totaldisclist totaldisc = resttemplate.getforobject(url, totaldisc.class); model.put("discvalue",totaldisc); return "salesorders/totaldisc"; }
gson way :
public string initcreationform(map<string, object> model) { string url = "http://localhost:8080/springservicejson/view/"; gson gson = new gson(); collection<totaldisc> totaldisc = gson.fromjson(url, piutanglistjson.class); model.put("discvalue",totaldisc); return "salesorders/totaldisc"; }
what missed here? give me error "could not extract response: no suitable httpmessageconverter found response type [class [lorg.springframework.samples.my.model.totaldisclist;] , content type [application/json]"
here object totaldisclist.java
public class totaldisclist { private string no; @datetimeformat(pattern="dd-mm-yyyy") private date date; private long grandtotal; private long diskon; //getter setter skipped }
i should return list<totaldisclist> totaldisc = resttemplate.getforobject(url, list<totaldisc>.class);
right? how properly?
edit
you need provide message converter resttemplate
resttemplate.getmessageconverters().add(new mappingjackson2httpmessageconverter()); resttemplate.getmessageconverters().add(new stringhttpmessageconverter());
and try using array, such as:
totaldisclist[] totaldisc = resttemplate .getforobject(url, totaldisclist[].class);
Comments
Post a Comment