Saakina
Saakina

Reputation: 285

Calling rest service with Spring Rest template

I have a service that calls consumes a specific URL. I have a class for the items and another class for the specific fields in the items. One of the fields is an array and I need to access it.

"items"[{
  "success":true,
  "publicKey":"dZ4EVmE8yGCRGx5XRX1W",
  "stream":{
    "_doc":{
      "tags":[
        "battery",
        "humidity",
        "light",
        "pressure",
        "rain",
        "temperature",
        "weather",
        "wind"
      ]
      "date":"2014-04-05T14:37:39.441Z",
      "last_push":"2014-09-12T18:22:26.252Z",
      "hidden":false,
      "flagged":false,
      "alias":"wimp_weather",
      "location":{
        "long":"Boulder, CO, United States",
        "city":"Boulder",
        "state":"Colorado",
        "country":"United States",
        "lat":"40.0149856",
        "lng":"-105.27054559999999"
      },
      "title":"Wimp Weather Station",
      "description":"Weather station on top my roof. Read the full tutorial here: https://learn.sparkfun.com/tutorials/weather-station-wirelessly-connected-to-wunderground",
      "__v":0,
      "_id":"534015331ebf49e11af8059d"
    },
  }
]
}

I'm trying to get success, publicKey and location. But the result is:

{"success":true,"publicKey":"dZ4EVmE8yGCRGx5XRX1W","location":null}

I use this method to retrieve the result

@RequestMapping("/weather")
public ResponseEntity result() {

    WeatherPOJO result = weatherService.fetchWeather();
    return new ResponseEntity(result.getItems().get(0), HttpStatus.OK);
}

And this is the class that fetches the url

public WeatherPOJO fetchWeather() {
    WeatherPOJO rest = restTemplate.getForObject(
            "https://data.sparkfun.com/streams/dZ4EVmE8yGCRGx5XRX1W.json"
            , WeatherPOJO.class);
    return rest;
}

WeatherItem class

package com.example.services;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown=true)
public class WeatherItem {

    private boolean success;
    private String publicKey;
    private String location;

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }


    public String getPublicKey() {
        return publicKey;
    }

    public void setPublicKey(String publicKey) {
        this.publicKey = publicKey;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }
}

WeatherPOJO

package com.example.services;

import java.util.List;

public class WeatherPOJO {

    private List<WeatherItem> items;

    public List<WeatherItem> getItems() {
        return items;
    }

    public void setItems(List<WeatherItem> items) {
        this.items = items;
    }
}

Why can't I reach the location value?

Upvotes: 1

Views: 782

Answers (1)

Andrew Wynham
Andrew Wynham

Reputation: 2398

Because location is nested further down. Try adding this to your WeatherItem:

@JsonProperty("stream")
public void setStream(Map<String, Object> nested) {
    this.location = nested.get("_doc").get("location").get("long");
}

Of course this will only get you the long name of the location into your location variable location. If you want all the fields then create a bean with them and then map the entire location object

private Location location;
...

@JsonProperty("stream")
public void setStream(Map<String, Object> nested) {
    Map<String, Object> loc = nested.get("_doc").get("location");
    this.location = new Location();
    this.location.setCity(loc.get("city"));
    ....
}

Of course the easiest thing would be to match your pojo to the structure (unrelated properties/getters/setters left out for brevity):

class WeatherItem {
    Stream stream;
}
class Stream {
    Doc _doc;
}
class Doc {
    Location location;
}
class Location {
    String city;
    Double lat;
    Double lon;
    ....
}

Upvotes: 1

Related Questions