Serialization errors due to jackson-databind version mismatch?

user_mda picture user_mda · Oct 26, 2015 · Viewed 8.2k times · Source

I am running into the following error

java.lang.NoSuchFieldError: WRITE_DURATIONS_AS_TIMESTAMPS
    at com.fasterxml.jackson.datatype.joda.ser.DurationSerializer.<init>(DurationSerializer.java:28)
    at com.fasterxml.jackson.datatype.joda.ser.DurationSerializer.<init>(DurationSerializer.java:25)
    at com.fasterxml.jackson.datatype.joda.JodaModule.<init>(JodaModule.java:45)

I checked to see what versions of jackson-datatype-joda are available. It appears that maven has excluded all version mismatches.

Any other reason this might cause serialization errors?

Answer

Ivan Hristov picture Ivan Hristov · Dec 9, 2015

The problem is that among the maven dependencies (mind that it could be a transitive one) you have incompatible versions of jackson-datatype-joda and jackson-databind. Incompatible in the sense that jackson-databind's SerializationFeature class is missing the WRITE_DURATIONS_AS_TIMESTAMPS field. To see what dependencies maven brings you can run the following command in the terminal (or you can use an IDE's maven plug to search and analyse the maven dependency tree):

mvn dependency:tree | grep databind

the outcome will most probably be something like:

[INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.4.1:compile

The version of course can vary but the important thing is that the WRITE_DURATIONS_AS_TIMESTAMPS field is only available since version 2.5

You can exclude a transitive dependency like this:

<dependency>
  <groupId>group.id</groupId>
  <artifactId>artifact-id</artifactId>
  <version>${artifact.version}</version>
  <exclusions>
    <exclusion>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
    </exclusion>
  </exclusions>
</dependency>

If it's not a transitive dependency you need to update version of jackson-databind.