android - Spring @ResponseBody: format JSON to put status -
i trying map pojos json jackson technology using spring create web service, have problem, when map list json creates malformed json like:
["idline":"a61dcedb-4a6b-4f8f-bbdd-32cc51e861b2", "name":"line1", "code":"pru1" ] i want create mapper, filter or similar response like:
{ status:"ok", data:[ {"idline":"a61dcedb-4a6b-4f8f-bbdd-32cc51e861b2", "name":"line1", "code":"pru1" }] } i have tryed objectmapper no result. thanks.
update line 1 parses , invokes malformed exception (it belongs external library)
result = (jsonobject) new jsontokener(str).nextvalue(); and json i'm trying parse is:
[{"idline":"a61dcedb-4a6b-4f8f-bbdd-32cc51e861b2","name":"linea1","code":"pru1","colour":"#ff3200","means":"bus","type":"urban","notes":"","state":true,"timetableinterurbans":[]}] to clarify, serialize pojo on spring using @responsebody , response retrieved android , parsed using jsonobject throws exception.
the server side method:
@requestmapping(value="/{country}/{cityname}/{type}", method=requestmethod.get) public @responsebody list<line> geturbanlinesfromcity(@pathvariable string country, @pathvariable string cityname, @pathvariable string type){ //get city pojo country , city name city city = citymanager.searchcitybycountryandname(country, cityname); //get lines city list<line> lines = null; if (city != null){ lines = linemanager.getlinesbycityandtype(city.getidcity(), type); } return lines; }
finally i've solved using helper class called jsonresponse. has 2 attributes, 1 means status of request , other means data response.
public class jsonresponse{ private object data; private boolean status; public jsonresponse (object data){ this.data = data; this.status = this.data != null; } //getters , setters both attributes serialized } now thing have serialize helper object data inside, getting consistent json response on multiple api calls using @requestbody jsonresponse returning value each method in every controller.
Comments
Post a Comment