40

我有提供所有国家/地区的休息网址 - 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);

当我运行此代码时,出现异常:

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

最后是我的国家课:

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

问题是返回的 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"
    }
]
}

如何告诉RestTemplate将数组的每个元素转换为Country对象?

4

5 回答 5

43

您需要执行以下操作:

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();

如果您可以使用某种注释来允许您跳过关卡,那就太好了,但目前还不可能(参见thisthis

于 2014-07-21T12:07:27.053 回答
9

另一种解决方案:

public class CountryInfoResponse {
  private List<Object> geonames;
}

使用通用Object -List 解决了我的问题,因为还有其他数据类型,如布尔值。

于 2015-02-05T16:59:08.193 回答
4

就我而言,我使用 JQuery 获得了价值,我是<input type="text">这样做的:

var newUserInfo = { "lastName": inputLastName[0].value, "userName": inputUsername[0].value,
 "firstName": inputFirstName[0] , "email": inputEmail[0].value}

而且我经常遇到这个异常

com.fasterxml.jackson.databind.JsonMappingException:无法从 [Source: java.io.PushbackInputStream@39cb6c98; 的 START_OBJECT 令牌中反序列化 java.lang.String 的实例;行:1,列:54](通过引用链:com.springboot.domain.User["firstName"])。

我撞了头一个小时,直到我意识到.value在这之后我忘了写"firstName": inputFirstName[0]

因此,正确的解决方案是:

var newUserInfo = { "lastName": inputLastName[0].value, "userName": inputUsername[0].value,
 "firstName": inputFirstName[0].value , "email": inputEmail[0].value}

我来这里是因为我遇到了这个问题,我希望我能帮别人省去几个小时的痛苦。

干杯:)

于 2017-05-31T17:31:42.090 回答
1

如果你想避免使用额外的ClassList<Object> genomes你可以简单地使用Map.

数据结构转化为Map<String, List<Country>>

String resourceEndpoint = "http://api.geonames.org/countryInfoJSON?username=volodiaL";

Map<String, List<Country>> geonames = restTemplate.getForObject(resourceEndpoint, Map.class);

List<Country> countries = geonames.get("geonames");
于 2018-09-04T16:53:50.240 回答
0

对于 Spring-boot 1.3.3, List 的方法 exchange() 与相关答案中的一样

Spring Data Rest - _links

于 2016-03-25T09:00:59.583 回答