Martin
Martin

Reputation: 862

NoSuchFieldError "ADJUST_DATES_TO_CONTEXT_TIME_ZONE" when trying to parse json

I want to convert a json string containing a date into DateTime of jodaTime using jackson. Unfortunately I get this error

java.lang.NoSuchFieldError: ADJUST_DATES_TO_CONTEXT_TIME_ZONE

The json object looks like this:

{
"add_time": "2017-04-26 14:26:58",
}

I have included joda time and jackson as follows in my pom.xml:

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

jackson version is 2.8.8. I've created my object mapper this way:

    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JodaModule());

Does anybody know what the problem might be? I'm stuck on this for a couple of hours now. I also tried do disable the DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE but it does not help.

Upvotes: 6

Views: 3918

Answers (4)

vanomart
vanomart

Reputation: 1839

In my case the issue was quite straightforward - from the stack trace I found the line in which the issue occurred in JacksonJodaDateFormat line 124:

Exception in thread "main" java.lang.NoSuchFieldError: ADJUST_DATES_TO_CONTEXT_TIME_ZONE
    at com.fasterxml.jackson.datatype.joda.cfg.JacksonJodaDateFormat.with(JacksonJodaDateFormat.java:124)
    at com.fasterxml.jackson.datatype.joda.ser.JodaDateSerializerBase.createContextual(JodaDateSerializerBase.java:95)
    at com.fasterxml.jackson.databind.SerializerProvider.handleSecondaryContextualization(SerializerProvider.java:956)
    at com.fasterxml.jackson.databind.SerializerProvider.findValueSerializer(SerializerProvider.java:536)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.resolve(BeanSerializerBase.java:320)
    at com.fasterxml.jackson.databind.ser.SerializerCache.addAndResolveNonTypedSerializer(SerializerCache.java:197)
    at com.fasterxml.jackson.databind.SerializerProvider._createAndCacheUntypedSerializer(SerializerProvider.java:1177)
    at com.fasterxml.jackson.databind.SerializerProvider.findValueSerializer(SerializerProvider.java:490)
    at com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap.findAndAddSecondarySerializer(PropertySerializerMap.java:90)
    at com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase._findAndAddDynamic(AsArraySerializerBase.java:312)

So after decompiling the file in IntelliJ Idea, I got this: Screenshot of decompiled Joda serializer class

I found out that my version of JsonFormat from com.fasterxml.jackson.annotation package doesn't contain the Feature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE. The missing value is actually in https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations package, so after I imported the package in maven (make sure you have the same version as your core Jackson import), it all started to work. Here's the import:

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.9.5</version>
</dependency>

Upvotes: 1

henoc
henoc

Reputation: 333

NoSuchFieldError results from class structure incompatibility of jackson library side, so you need to check the finally resolved library versions of jackson series.

Upvotes: 1

Turboman
Turboman

Reputation: 1

Maybe you need a deserializer for ZonedDateTime? I got the same error as you and solved it with the following code (note: I used Jackson 2.9.2):

SimpleModule module = new SimpleModule();
module.addDeserializer(ZonedDateTime.class, new ZonedDateTimeDeserializer());
mapper.registerModule(module);

The ZonedDateTimeDeserializer:

public class ZonedDateTimeDeserializer extends StdDeserializer<ZonedDateTime> {
...
@Override
public ZonedDateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonNode node = jp.getCodec().readTree(jp);
    String zonedDateTime = node.textValue();
    return ZonedDateTime.parse(zonedDateTime, DateTimeFormatter.ofPattern("yyyy-MM-dd' 'HH:mm:ss"));
 }
} 

(I realize I am slightly late for this question, but I hope it can help someone)

Upvotes: 0

Manos Nikolaidis
Manos Nikolaidis

Reputation: 22254

You need to specify a custom date time format as the one in your JSON is not what Jackson expects. You can do that easily by a @JsonFormat annotation over the field or setter method (No need to disable ADJUST_DATES_TO_CONTEXT_TIME_ZONE). E.g.

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
DateTime addTime;

The date in your example will be parsed as 2017-04-26T14:26:58.000Z. Which by the way is the default format Jackson expected.

You may look into custom deserializers if you don't want to add annotations but that's more complicated.

Upvotes: 0

Related Questions