当前位置: 首页 > 知识库问答 >
问题:

Jackson/RestTemplate Springboot反序列化数组JSON

王修为
2023-03-14

我似乎很专注于如何去硬化数组数组。我对这些工具很陌生,无法处理JSON文件,我能够去硬化来自国家(https://restcountries.eu/rest/v2/all)的REST应用编程接口的JSON,但我现在正试图从开放天空应用编程接口(https://opensky-network.org/api/states/all?lamin=45.8389

我有这门课:

@JsonIgnoreProperties
public class Plane {

    private String icao24;
    private String callsign;
    private String origin_country;
    private int time_position;
    private int last_contact;
    private float longitude;
    private float latitude;
    private float baro_altitude;
    private boolean on_ground;
    private float velocity;
    private float true_track;
    private float vertical_rate;
    private int[] sensors;
    private float geo_altitude;
    private String squawk;
    private boolean spi;
    private int position_source;
}

有能手和二传手

还有这个:

@JsonIgnoreProperties
public class SkyResponseObject {

    private Plane[][] states;

}
and this method that does the API call:

    public SkyResponseObject getJson(String parameters){
        return restTemplate.getForObject(URL+parameters,SkyResponseObject.class);
    }

但杰克逊似乎在抛出错误,已经尝试了多种组合的东西,但无法让它发挥作用,一些帮助将不胜感激:

抛出错误的示例:

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonKey
    at com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector.hasAsKey(JacksonAnnotationIntrospector.java:1080)
    at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector._addFields(POJOPropertiesCollector.java:495)
    at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector.collectAll(POJOPropertiesCollector.java:421)
    at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector.getPropertyMap(POJOPropertiesCollector.java:386)
    at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector.getProperties(POJOPropertiesCollector.java:233)
    at com.fasterxml.jackson.databind.introspect.BasicBeanDescription._properties(BasicBeanDescription.java:164)
    at com.fasterxml.jackson.databind.introspect.BasicBeanDescription.findProperties(BasicBeanDescription.java:239)
    at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory._findCreatorsFromProperties(BasicDeserializerFactory.java:328)
    at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory._constructDefaultValueInstantiator(BasicDeserializerFactory.java:272)
    at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory.findValueInstantiator(BasicDeserializerFactory.java:223)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:261)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:150)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:414)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:349)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:264)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
    at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
    at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:591)
    at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:4733)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4594)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3601)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:378)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:342)
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:105)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:778)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:711)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:334)
    at com.labproject.SkyTracker.OpenSky.OpenSkyController.getJson(OpenSkyController.java:25)
    at com.labproject.SkyTracker.SkyTrackerApplication.main(SkyTrackerApplication.java:23)

编辑:完成评论中的建议后,我的错误是:

Exception in thread "main" org.springframework.web.client.RestClientException: Error while extracting response for type [class com.labproject.SkyTracker.OpenSky.SkyResponseObject] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize a POJO (of type `com.labproject.SkyTracker.OpenSky.Plane`) from non-Array representation (token: VALUE_STRING): type/property designed to be serialized as JSON Array; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize a POJO (of type `com.labproject.SkyTracker.OpenSky.Plane`) from non-Array representation (token: VALUE_STRING): type/property designed to be serialized as JSON Array
 at [Source: (PushbackInputStream); line: 1, column: 31] (through reference chain: com.labproject.SkyTracker.OpenSky.SkyResponseObject["states"]->java.lang.Object[][0]->java.lang.Object[][0])
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:120)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:778)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:711)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:334)
    at com.labproject.SkyTracker.OpenSky.OpenSkyController.getJson(OpenSkyController.java:25)
    at com.labproject.SkyTracker.SkyTrackerApplication.main(SkyTrackerApplication.java:23)
Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize a POJO (of type `com.labproject.SkyTracker.OpenSky.Plane`) from non-Array representation (token: VALUE_STRING): type/property designed to be serialized as JSON Array; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize a POJO (of type `com.labproject.SkyTracker.OpenSky.Plane`) from non-Array representation (token: VALUE_STRING): type/property designed to be serialized as JSON Array
 at [Source: (PushbackInputStream); line: 1, column: 31] (through reference chain: com.labproject.SkyTracker.OpenSky.SkyResponseObject["states"]->java.lang.Object[][0]->java.lang.Object[][0])
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:389)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:342)
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:105)
    ... 5 more
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize a POJO (of type `com.labproject.SkyTracker.OpenSky.Plane`) from non-Array representation (token: VALUE_STRING): type/property designed to be serialized as JSON Array
 at [Source: (PushbackInputStream); line: 1, column: 31] (through reference chain: com.labproject.SkyTracker.OpenSky.SkyResponseObject["states"]->java.lang.Object[][0]->java.lang.Object[][0])
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1601)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1375)
    at com.fasterxml.jackson.databind.deser.impl.BeanAsArrayDeserializer._deserializeFromNonArray(BeanAsArrayDeserializer.java:376)
    at com.fasterxml.jackson.databind.deser.impl.BeanAsArrayDeserializer.deserialize(BeanAsArrayDeserializer.java:105)
    at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:214)
    at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:24)
    at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:214)
    at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:24)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:402)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:195)
    at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4593)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3601)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:378)
    ... 7 more

EDIT2:忘了举个例子:

{"time":1616687149,"states":[["4b1806","SWR141W ","Switzerland",1616687090,1616687145,8.5609,47.4424,null,true,0,244.69,null,null,null,null,false,0]]}

共有1个答案

姬选
2023-03-14

在你的pom.xml

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
 类似资料:
  • 我正在尝试反序列化列表通量 我已经研究了这个问题,它涉及一系列对象。事实上,我已经能够使用他们的Mono解决方案方法来检索Mono 当我尝试使用Flux而不是Mono时会发生什么:我已经尝试过使用WebClient和Mign-两者的效果相同。单步执行Jackson代码,在处理Flux世界时它永远不会拉回TokenBuffers。这似乎与Codecs和Jackson JSON的Spring留档中的内

  • 我收到来自第3方服务提供商的JSON响应,其中包含一系列对象。当我尝试使用Jackson api反序列化JSON时。我收到以下异常 我的回答是 我的POJO课是这样的 我正在尝试使用以下代码反序列化JSON 如果我试着去做 它在BEGIN_对象本身失败。 如何使用数组读取和反序列化JSON。我应该编写自己的反序列化器吗? 编辑如果我使用JSON字符串而不是流,那么我就能够取回所有Java对象。但为

  • 我正在阅读Facebook的洞察,并试图让Jackson将JSON映射到Object。如果所有的数据都不是空的,我就会让它正常工作。但是我在尝试反序列化键值的空数组时遇到了问题。即使尝试了这篇文章:如何防止Map内部的null值和bean内部的null字段通过Jackson序列化,也没有解决问题:( 这是JSON: 我的代码段如下: 全堆栈跟踪:

  • 我有以下JSON文件要反序列化

  • 我在试着读我的。json文件。Is是一个车辆存储类。 这是错误: com.fasterxml.jackson.databind.exc.MismatchedInputException:无法构造的实例(尽管至少存在一个Creator):无法构造的实例(尽管至少存在一个Creator):没有字符串参数构造函数/工厂方法来从[Source:(File); line: 1,列: 1]处的字符串值反序列化

  • 我使用Json-Jackson序列化/反序列化我的对象,并且与内置类配合得很好。 字符串、int等非常好。但是现在我必须序列化/反序列化以特定方式构建的对象。遵循声明: 如果我使用标准的序列化器/反序列化,它会向我显示一条错误消息,例如“java.lang.不支持的操作异常...”,因此它无法序列化/反序列化bobjs。 我如何以一般方式做到这一点,所以我不写3个序列化器/反序列化器,而是只告诉J