使用Spring Rest模板调用休息服务

时间:2017-03-15 12:31:28

标签: java spring rest

我有一个服务,调用消耗特定的URL。我有一个项目类和另一个类项目中的特定字段。其中一个字段是一个数组,我需要访问它。

"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"
    },
  }
]
}

我试图获得成功,publicKey和位置。但结果是:

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

我使用此方法检索结果

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

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

这是获取网址的类

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

WeatherItem类

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;
    }
}

为什么我无法达到位置值?

1 个答案:

答案 0 :(得分:1)

因为位置进一步嵌套。尝试将其添加到WeatherItem

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

当然,这只会让您获得位置变量位置的长名称。如果你想要所有的字段然后用它们创建一个bean,然后映射整个位置对象

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"));
    ....
}

当然最简单的方法是将你的pojo与结构相匹配(为了简洁而省略了不相关的属性/ getters / setter):

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