0
我使用jackson依賴項來序列化包含一些Java Instant字段的請求對象。java即時使用jackson不會獲得序列化爲毫秒
以下是我mavan依賴傑克遜
<!-- 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 shaded jar將所有jar放入單獨的陰影罐中。我們使用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);
}
有人可以幫我解決這個問題。
'findAndRegisterModules'無法找到jsr310模塊。看起來依賴關係不在類路徑中。檢查你的陰影罐,看看這個依賴關係是否存在。 – Veeram