Unable to map json string to java object using jackson API -
i writing web service (server + client). able make service , returns me following json
{ "cities": { "city": [ { "name": "new delhi", "population": "19m", "telephonecode": "011" }, { "name": "mumbai", "population": "21m", "telephonecode": "022" }, { "name": "chennai", "population": "10m", "telephonecode": "044" } ] } }
my pojo's
@xmlrootelement(name = "cities") public class restfulcities { list<restfulcity> restfulcitylist; @xmlelement(name = "city") public list<restfulcity> getrestfulcitylist() { return restfulcitylist; } public void setrestfulcitylist(list<restfulcity> restfulcitylist) { this.restfulcitylist = restfulcitylist; } } @xmlrootelement(name = "city") public class restfulcity { private string name; private string telephonecode; private string population; public restfulcity(string name, string telephonecode, string population) { this.name = name; this.telephonecode = telephonecode; this.population = population; } public restfulcity(city city) { this.name = city.getname(); this.telephonecode = city.gettelephonecode(); this.population = city.getpopulation(); } @xmlelement public string getname() { return name; } @xmlelement public string gettelephonecode() { return telephonecode; } @xmlelement public string getpopulation() { return population; } } now want write client map json pojo restfulcities object populated in java
my client code below:
public class client { static final string rest_uri = "http://localhost:8080/springrest/rest/"; static final string cities = "cities"; public static void main(string[] args) { string s = ""; webclient plainaddclient = webclient.create(rest_uri); plainaddclient.path(cities).accept("application/json"); s = plainaddclient.get(string.class); try { restfulcities citiesobject = new objectmapper().readvalue(s, restfulcities.class); for(restfulcity city : citiesobject.getrestfulcitylist()) { system.out.println("----------start---------"); system.out.println(city.getname()); system.out.println(city.getpopulation()); system.out.println(city.gettelephonecode()); system.out.println("---------end----------"); } } catch (jsonparseexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (jsonmappingexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } } but issue is: getting following exception
org.codehaus.jackson.map.exc.unrecognizedpropertyexception: unrecognized field "cities"(class com.techartifact.example.spring.model.restfulcities), not marked ignorable @ [source: java.io.stringreader@1d35bf2; line: 1, column: 12] (through reference chain: com.techartifact.example.spring.model.restfulcities["cities"]) when using below property:
@jsonignoreproperties(ignoreunknown = true) although donot exception, restfulcitylist null not desired
please help
you using jaxb annotations need configure objectmapper correct module; need jackson-module-jaxb-annotations project. add using favorite dependency management system , use this:
jaxbannotationmodule module = new jaxbannotationmodule(); // configure necessary objectmapper.registermodule(module); note: jackson 2.x. jackson 1.x support jaxb default, version not supported anymore , given having problem using jackson 2.x anyways.
update: jaxb annotation great interop xml system, should use jackson's own annotation if can. remove need jaxb module , configuration of objectmapper. also, there functionality in jackson available through annotation because there no equivalent in jaxb.
Comments
Post a Comment