Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to map json string to java object using jackson API
    primarykey
    data
    text
    <p>I am writing a web service (server + client). I am able to make a service and it returns me the following json</p> <pre><code>{ "cities": { "city": [ { "name": "New Delhi", "population": "19M", "telephonecode": "011" }, { "name": "Mumbai", "population": "21M", "telephonecode": "022" }, { "name": "Chennai", "population": "10M", "telephonecode": "044" } ] } </code></pre> <p>}</p> <p>my POJO's are </p> <pre><code>@XmlRootElement(name = "cities") public class RestFulCities { List&lt;RestFulCity&gt; restFulCityList; @XmlElement(name = "city") public List&lt;RestFulCity&gt; getRestFulCityList() { return restFulCityList; } public void setRestFulCityList(List&lt;RestFulCity&gt; 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; } } </code></pre> <p>Now I want to write a client which will map this json to my POJO so that I get a RestFulCities Object populated in java</p> <p>My client code is below: </p> <pre><code>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(); } } } </code></pre> <p>But the issue is: I am getting the following exception</p> <pre><code>org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "cities"(Class com.techartifact.example.spring.model.RestFulCities), not marked as ignorable at [Source: java.io.StringReader@1d35bf2; line: 1, column: 12] (through reference chain: com.techartifact.example.spring.model.RestFulCities["cities"]) </code></pre> <p>When I am using below property:</p> <pre><code>@JsonIgnoreProperties(ignoreUnknown = true) </code></pre> <p>Although I donot get the exception, but my <strong>restFulCityList is null</strong> which is not desired</p> <p>Please help</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload