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

时间:2021-10-15 20:20:37

I have read many postings on here on JSON parsing to Java objects and have had my parsing working just fine until I introduced LocalDateTime. I have tried to using the Java 8 parser, the JSR310 module and to build a customization - below describing the roadblocks on each. Any help would be appreciated!

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

This is my JSON string, creating by Jackson from another POJO:

这是我的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"}
    }
}

which generates

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)
...

called from:

ComputeArrive response = mapper.readValue(responseString, ComputeArrive.class);

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

Response has the following fields:

响应包含以下字段:

Logger logger // should not be mapped by JSON, no getters/setters static final long serialVersionUID = 1L; // " String validEscortsWTheirSpecReqs = "YES" // want mapped LocalDateTime modifiedDateTimeNeedToBeThere // want mapped

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

I have probably registered too many modules by now, adding them in to try to get it to parse:

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

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

So I have tried to add Spring Boot dependencies to my project and write a customizer for JSR310 (which seems unecessary given the modules above):

所以我试图将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;
    }
}

My application has numerous dependencies so I did not want to make anything a "parent" of it and added the following dependencies. However, the class "Jackson2ObjectMapperBuilder" is not being found so the above does not compiles with these dependencies:

我的应用程序有很多依赖项,所以我不想让它成为它的“父”,并添加了以下依赖项。但是,没有找到“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>   

I am not finding a lot of documentation for "Jackson2ObjectMapperBuilderCustomizer" which may be a replacement for "Jackson2ObjectMapperBuilder" - which could be the issue. Please note that the class above is not instantiated by me because I assume Spring instantiates it.

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

Any thoughts on how to parse my response would be most appreciated!

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

1 个解决方案

#1


0  

For me, this problem was solved by adding the JavaTimeModule to the Jackson2ObjectMapperBuilder like this:

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

@Configuration
public class MyConfiguration {

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

}

Then Spring uses this mapper with the registered module.

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

#1


0  

For me, this problem was solved by adding the JavaTimeModule to the Jackson2ObjectMapperBuilder like this:

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

@Configuration
public class MyConfiguration {

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

}

Then Spring uses this mapper with the registered module.

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