使用Jackson“意外令牌(START_OBJECT)使用LocalDateTime将JSON解析为POJO,预期VALUE_STRING:预期的数组或字符串。”

问题描述 投票:7回答:1

我已经阅读了很多关于JSON解析到Java对象的帖子,并且在我引入LocalDateTime之前,我的解析工作正常。我曾尝试使用Java 8解析器,JSR310模块并构建自定义 - 下面描述每个解决方案的障碍。任何帮助,将不胜感激!

这是我的JSON字符串,由杰克逊从另一个POJO创建:

{"validEscortsWTheirSpecReqs":"MAYBE",
 "modifiedDateTimeNeedToBeThere":     
    {"dayOfMonth":6,"dayOfWeek":"MONDAY","month":"FEBRUARY","year":2017,
     "hour":10,"minute":1,"second":24,"nano":0,"dayOfYear":37,"monthValue":2,
     "chronology":{"id":"ISO","calendarType":"iso8601"}
    }
}

产生

com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (START_OBJECT), expected VALUE_STRING: Expected array or string. at 
...
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
    at com.fasterxml.jackson.databind.DeserializationContext.wrongTokenException(DeserializationContext.java:1376)
    at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:118)
    at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:39)
...

来自:

ComputeArrive响应= mapper.readValue(responseString,ComputeArrive.class);

响应具有以下字段:

记录器记录器//不应该由JSON映射,没有getters / setters静态最终长serialVersionUID = 1L; //“String validEscortsWTheirSpecReqs =”YES“//想要映射的LocalDateTime modifiedDateTimeNeedToBeThere //想要映射

我现在可能已经注册了太多的模块,添加它们以试图让它解析:

    mapper.findAndRegisterModules();
    //mapper           .registerModule(new ParameterNamesModule());
    mapper.registerModule(new Jdk8Module());
    mapper.registerModule(new JavaTimeModule());
    mapper.registerModule(new JSR310Module());

所以我试图将Spring Boot依赖项添加到我的项目中,并为JSR310编写一个自定义程序(鉴于上述模块,这似乎是不必要的):

@Configuration
public class JacksonConfig {

    @Bean
    @Primary
    public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        objectMapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
        return objectMapper;
    }
}

我的应用程序有很多依赖项,所以我不想让它成为它的“父”,并添加了以下依赖项。但是,找不到“Jackson2ObjectMapperBuilder”类,所以上面没有编译这些依赖项:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.3.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.3.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.3.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>4.3.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>4.3.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>aopalliance</groupId>
    <artifactId>aopalliance</artifactId>
    <version>1.0</version>
</dependency>   

我没有找到很多关于“Jackson2ObjectMapperBuilderCustomizer”的文档,它可能是“Jackson2ObjectMapperBuilder”的替代品 - 这可能就是问题所在。请注意,上面的类没有被我实例化,因为我假设Spring实例化它。

任何关于如何解析我的回答的想法都将非常感激!

java json spring parsing
1个回答
0
投票

对我来说,这个问题通过将JavaTimeModule添加到Jackson2ObjectMapperBuilder来解决,如下所示:

@Configuration
public class MyConfiguration {

    @Bean
    @Primary
    public Jackson2ObjectMapperBuilder objectMapperBuilder() {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        return builder.modulesToInstall(new JavaTimeModule());
    }

}

然后Spring将此映射器与已注册的模块一起使用。

© www.soinside.com 2019 - 2024. All rights reserved.