用Spring rest模板调用rest服务的java
我有一个服务,它调用一个特定的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"
},
}
]
}
我正在努力获得成功,公开密钥和位置。但结果是:
{"success":true,"publicKey":"dZ4EVmE8yGCRGx5XRX1W","location":null}
我使用这个方法来检索结果
@RequestMapping("/weather")
public ResponseEntity result() {
WeatherPOJO result = weatherService.fetchWeather();
return new ResponseEntity(result.getItems().get(0), HttpStatus.OK);
}
这是获取url的类
public WeatherPOJO fetchWeather() {
WeatherPOJO rest = restTemplate.getForObject(
"https://data.sparkfun.com/streams/dZ4EVmE8yGCRGx5XRX1W.json"
, WeatherPOJO.class);
return rest;
}
天气项目类
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;
}
}
韦瑟波乔
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;
}
}
为什么我不能达到位置值
共 (0) 个答案