java 使用Jackson“意外令牌(START_OBJECT),预期VALUE_STRING:预期数组或字符串”使用LocalDateTime将JSON解析为POJO。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41617715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing JSON to POJO with LocalDateTime using Hymanson "Unexpected token (START_OBJECT), expected VALUE_STRING: Expected array or string."
提问by Dr Dave
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 Hymanson from another POJO:
这是我的 JSON 字符串,由 Hymanson 从另一个 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.Hymanson.databind.JsonMappingException: Unexpected token (START_OBJECT), expected VALUE_STRING: Expected array or string. at
...
at com.fasterxml.Hymanson.databind.JsonMappingException.from(JsonMappingException.java:270)
at com.fasterxml.Hymanson.databind.DeserializationContext.wrongTokenException(DeserializationContext.java:1376)
at com.fasterxml.Hymanson.datatype.jsr310.deser.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:118)
at com.fasterxml.Hymanson.datatype.jsr310.deser.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:39)
...
called from:
来自:
ComputeArrive response = mapper.readValue(responseString, ComputeArrive.class);
ComputeArrive response = 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
Logger logger // 不应该被 JSON 映射,没有 getter/setter static final 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 HymansonConfig {
@Bean
@Primary
public ObjectMapper objectMapper(Hymanson2ObjectMapperBuilder 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 "Hymanson2ObjectMapperBuilder" is not being found so the above does not compiles with these dependencies:
我的应用程序有很多依赖项,所以我不想让任何东西成为它的“父级”,并添加了以下依赖项。但是,未找到类“Hymanson2ObjectMapperBuilder”,因此上述内容不会使用这些依赖项进行编译:
<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 "Hymanson2ObjectMapperBuilderCustomizer" which may be a replacement for "Hymanson2ObjectMapperBuilder" - which could be the issue. Please note that the class above is not instantiated by me because I assume Spring instantiates it.
我没有找到很多关于“Hymanson2ObjectMapperBuilderCustomizer”的文档,这可能是“Hymanson2ObjectMapperBuilder”的替代品——这可能是问题所在。请注意,上面的类不是我实例化的,因为我假设 Spring 实例化了它。
Any thoughts on how to parse my response would be most appreciated!
任何关于如何解析我的回复的想法将不胜感激!
回答by Tobias
For me, this problem was solved by adding the JavaTimeModule
to the Hymanson2ObjectMapperBuilder
like this:
对于我来说,这个问题解决了通过加入JavaTimeModule
到Hymanson2ObjectMapperBuilder
这样的:
@Configuration
public class MyConfiguration {
@Bean
@Primary
public Hymanson2ObjectMapperBuilder objectMapperBuilder() {
Hymanson2ObjectMapperBuilder builder = new Hymanson2ObjectMapperBuilder();
return builder.modulesToInstall(new JavaTimeModule());
}
}
Then Spring uses this mapper with the registered module.
然后 Spring 将这个映射器与注册的模块一起使用。
回答by Hod Ben Or
I think the problem is the parsing of the LocalDate, try this:
我认为问题是LocalDate的解析,试试这个:
{
"validEscortsWTheirSpecReqs":"MAYBE",
"modifiedDateTimeNeedToBeThere":[6,2,2017]
}
day, month, year represented by integers.
日、月、年用整数表示。