I read a lot of posts here about JSON analysis on Java objects and worked great with parsings until I introduced LocalDateTime. I tried to use the Java 8 parser, the JSR310 module and create a customization - below describing the roadblocks on each. Any help would be appreciated!
This is my JSON line created by Jackson from another 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);
The answer has the following fields:
Logger logger// JSON, /
serialVersionUID = 1L;// " validEscortsWTheirSpecReqs =" YES"//
LocalDateTime modifiedDateTimeNeedToBeThere//
, , , , :
mapper.findAndRegisterModules();
//mapper .registerModule(new ParameterNamesModule());
mapper.registerModule(new Jdk8Module());
mapper.registerModule(new JavaTimeModule());
mapper.registerModule(new JSR310Module());
, Spring 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 .
, , .