Vol*_*kyi 37 java spring resttemplate fasterxml
我有所有国家的休息网址 - http://api.geonames.org/countryInfoJSON?username=volodiaL.
我使用Spring 3中的RestTemplate将返回的json解析为java对象:
RestTemplate restTemplate = new RestTemplate();
Country[] countries = restTemplate.getForObject("http://api.geonames.org/countryInfoJSON?username=volodiaL",Country[].class);
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我得到一个例外:
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of hello.Country[] out of START_OBJECT token
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@1846149; line: 1, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:691)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:685)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.handleNonArray(ObjectArrayDeserializer.java:222)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:133)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:18)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2993)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2158)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:225)
... 7 more
Run Code Online (Sandbox Code Playgroud)
最后我的国家班:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Country {
private String countryName;
private long geonameId;
public String getCountryName() {
return countryName;
}
public long getGeonameId() {
return geonameId;
}
@Override
public String toString() {
return countryName;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是返回的json包含根元素"geonames",其中包含国家元素数组,如下所示:
{
"geonames": [
{
"continent": "EU",
"capital": "Andorra la Vella",
"languages": "ca",
"geonameId": 3041565,
"south": 42.42849259876837,
"isoAlpha3": "AND",
"north": 42.65604389629997,
"fipsCode": "AN",
"population": "84000",
"east": 1.7865427778319827,
"isoNumeric": "020",
"areaInSqKm": "468.0",
"countryCode": "AD",
"west": 1.4071867141112762,
"countryName": "Andorra",
"continentName": "Europe",
"currencyCode": "EUR"
}
]
}
Run Code Online (Sandbox Code Playgroud)
如何判断RestTemplate将数组的每个元素转换为Country对象?
geo*_*and 42
您需要执行以下操作:
public class CountryInfoResponse {
@JsonProperty("geonames")
private List<Country> countries;
//getter - setter
}
RestTemplate restTemplate = new RestTemplate();
List<Country> countries = restTemplate.getForObject("http://api.geonames.org/countryInfoJSON?username=volodiaL",CountryInfoResponse.class).getCountries();
Run Code Online (Sandbox Code Playgroud)
这将是巨大的,如果你可以使用一些类型的注释,让您可以跳级,但它没有可能(见本和本)
另一种方案:
public class CountryInfoResponse {
private List<Object> geonames;
}
Run Code Online (Sandbox Code Playgroud)
使用通用Object -List解决了我的问题,因为还有像Boolean这样的其他数据类型.
| 归档时间: |
|
| 查看次数: |
189684 次 |
| 最近记录: |