gpt4 book ai didi

java - RestTemplate:无法从 START_OBJECT token 中反序列化 OBJECT 的实例

转载 作者:行者123 更新时间:2023-11-30 11:09:39 26 4
gpt4 key购买 nike

我正在试用 Spring 的 RestTemplate 东西。我正在尝试读取此 JSON 数据:JSON Data .数据是键值对,其中键是“geonames”,值是“geoname”对象数组。

我有一个 Geoname 类来处理输入。这个类也有 getter 和 setter。然后我有一个应用程序类,它只运行一个主要方法来调用 RestTemplate 对象:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Geoname {
private String name;
private long lat;
private long lng;
private String countrycode;
}

应用程序.java

public class App 
{
public static void main( String[] args )
{
String jsonUrl = "http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo";

RestTemplate template = new RestTemplate();
ResponseEntity<Geoname[]> entity = template.getForEntity(jsonUrl, Geoname[].class);
List<Geoname> data = Arrays.asList(entity.getBody());

System.out.print("Success!");
}
}

这是我的错误输出:

Exception in thread "main" org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of com.declan.Geoname[] out of START_OBJECT token
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@54fc3ac5; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.declan.Geoname[] out of START_OBJECT token
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@54fc3ac5; line: 1, column: 1]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:208)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:200)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:96)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:812)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:796)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:576)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:529)
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:261)
at com.declan.App.main(App.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.declan.Geoname[] out of START_OBJECT token
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@54fc3ac5; line: 1, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:835)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:831)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.handleNonArray(ObjectArrayDeserializer.java:232)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:139)
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:17)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3560)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2660)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:205)
... 13 more

我试过这个设置,我试过像在 spring 文档中那样使用 getForObject,我试过在 stackoverflow 上搜索这里并使用映射到列表的常见答案。我什至尝试创建一个 Geonames 类,它只包含一个数组或 Geoname 对象,但也没有用……一直都是同样的错误。也许,我没有正确阅读 Json,但如果有人能借给我一双眼睛,我将不胜感激。 :D

干杯!

编辑

好的,我现在有了这个新类,它现在对 JSON 数据发出 GET 请求。但是,在调试之后,ResponseEntity 主体将数组设置为 null。我需要在 Geonames 中手动实例化数组吗?

@JsonIgnoreProperties(ignoreUnknown = true)
public class Geonames {
@JsonProperty("geonames")
Geoname[] geonames;

public void setGeonames(Geonames[] geonames) {
this.geonames = geonames;
}

public void getGeonames() {
return geonames;
}
}

最佳答案

已解决。结果是 null 是因为 JSON 链接有每小时最大点击次数限制。通过在网站上创建我自己的帐户,api 给了我自己的限制。因此,响应主体随后会填充数据。

关于java - RestTemplate:无法从 START_OBJECT token 中反序列化 OBJECT 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28055153/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com