Java – nosuchfielderror “adjust_dates_to_context_time_zone” when trying to parse JSON
I want to use Jackson to convert the JSON string containing the date to the datetime of jodatime Unfortunately, I received this error
java.lang.NoSuchFieldError: ADJUST_DATES_TO_CONTEXT_TIME_ZONE
The JSON object is as follows:
{ "add_time": "2017-04-26 14:26:58",}
I'm in my POM The XML contains joda time and Jackson:
<dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.9.9</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-joda</artifactId> <version>${jackson.version}</version> </dependency>
The Jackson version is 2.8 8. I created my object mapper in this way:
ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JodaModule());
Who knows what the problem may be? I've been holding on for hours now I also tried to disable the deserialization feature ADJUST_ DATES_ TO_ CONTEXT_ TIME_ Zone, but it doesn't help
Solution
You need to specify a custom date time format because the format in JSON is not what Jackson expected You can do this easily with the @ jsonformat annotation on the field or setter method (without disabling adjust_dates_to_context_time_zone) For example
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") DateTime addTime;
The date in your example will be resolved as 2017-04-26t14:26:58.000z By the way, Jackson expects the default format
If you don't want to add comments but are more complex, you can see the custom deserializer