我已经阅读了很多关于JSON解析到Java对象的帖子,并且在我引入LocalDateTime之前,我的解析工作正常。我曾尝试使用Java 8解析器,JSR310模块并构建自定义 - 下面描述每个解决方案的障碍。任何帮助将不胜感激!
这是我的JSON字符串,由Jackson从另一个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映射,也不应该由getter / setter映射 static final long 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实例化它。
对于如何解析我的回复的任何想法都将非常感激!
答案 0 :(得分:1)
我认为问题在于LocalDate的解析,请尝试以下操作:
{
"validEscortsWTheirSpecReqs":"MAYBE",
"modifiedDateTimeNeedToBeThere":[6,2,2017]
}
日,月,年,以整数表示。
答案 1 :(得分:0)
对我来说,通过将JavaTimeModule
添加到Jackson2ObjectMapperBuilder
就可以解决此问题:
@Configuration
public class MyConfiguration {
@Bean
@Primary
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
return builder.modulesToInstall(new JavaTimeModule());
}
}
然后Spring将此映射器与已注册的模块一起使用。
答案 2 :(得分:-1)
正如@Tobias 所说,使用 JavaTimeModule
将解决问题。
在您的 pom xml 中添加 JSR310 依赖项,以允许 Jackson 能够识别 Java 8 Date&Time API 数据类型:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
您现在可以像这样在 Jackson 配置中使用 JavaTimeModule
:
@Configuration
public class JacksonConfig {
@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
return objectMapper;
}
}
您应该使用此配置来序列化和反序列化 LocalDate 或其他 Java 8 Date&Time API 数据类型。