0

I am using Jackson dependencies for serializing my request object which contains some Java Instant fields.

Following are my Maven dependencies for Jackson

<!-- jackson dependecies -->
    <dependency>
      <groupId>com.fasterxml.jackson.datatype</groupId>
      <artifactId>jackson-datatype-jsr310</artifactId>
      <version>2.8.5</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.8.5</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.5</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.8.5</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.8.5</version>
    </dependency>
    <dependency>
         <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jdk8</artifactId>
        <version>2.8.5</version>
    </dependency>
    <!-- Jackson dependencies end -->

I am calling RESTful service from my code using jaxrs, below are corresponding Maven dependencies.

<!-- Jersey RESTful Services -->
        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-spring3</artifactId>
            <version>2.15</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-core</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-web</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-beans</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- Jersey json -->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>2.15</version>
        </dependency>
    <!-- End  -->

We use Apache shaded jar to put all the jars into single shaded jar. we use Java8 to run the shaded jar. When serializing the objects the java Instant is not getting serialized as milliseconds but nanoseconds. i.e.

"busDate":{"nano":0,"epochSecond":1482796800} instead of
"busDate":1482796800

We are setting below global settings on ObjectMapper class.

private static ObjectMapper staticMaper;

static {
    staticMaper = new ObjectMapper();
    staticMaper.findAndRegisterModules();
    staticMaper.setSerializationInclusion(Include.NON_NULL);
    staticMaper.disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);
    staticMaper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
    staticMaper.disable(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS);
    staticMaper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
    staticMaper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    staticMaper.enable(JsonGenerator.Feature.ESCAPE_NON_ASCII);
}

How can I resolve this issue?

halfer
  • 19,824
  • 17
  • 99
  • 186
Shantanoo K
  • 765
  • 5
  • 15
  • 43
  • `findAndRegisterModules` is not able to locate jsr310 module. Looks like the dependency is not in the classpath. Check your shaded jar to see if this dependency is there. – s7vr Jan 04 '17 at 21:04

1 Answers1

1

I have added the below line in my code.

objectMapper.registerModule(new JavaTimeModule()); Looks like sometimes this line --> objectMapper.findAndRegisterModules(); does not work as expected.

Shantanoo K
  • 765
  • 5
  • 15
  • 43