使用jackson

时间:2017-01-04 19:34:15

标签: json serialization jackson maven-shade-plugin

我使用jackson依赖项来序列化包含一些Java Instant字段的请求对象。

以下是我对杰克逊的依赖

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

我使用jaxrs从我的代码调用RESTful服务,下面是相应的maven依赖项。

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

我们使用Apache阴影罐将所有罐子放入单个阴影罐中。我们使用Java8来运行着色jar。序列化对象时,java Instant不会被序列化为毫秒而是纳秒。即。

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

我们在ObjectMapper类的全局设置下设置。

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);
}

有人可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

我在代码中添加了以下行。

objectMapper.registerModule(new JavaTimeModule()); 看起来有时候这一行 - &gt; objectMapper.findAndRegisterModules();无效。{/ p>