java - Jackson + Jersey 没有正确序列化日期
问题描述
我正在使用 Jersey + Jackson 开发 REST API,但我遇到了日期序列化问题。
你看,我JacksonJaxbJsonProvider
是这样配置的:
@Provider
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class JacksonProvider extends JacksonJaxbJsonProvider {
public static final ObjectMapper mapper;
static {
mapper = new ObjectMapper()
// DateUtils.ISO_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
.setDateFormat(new SimpleDateFormat(DateUtils.ISO_DATE_FORMAT, Locale.getDefault()))
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
public JacksonProvider() {
super();
setMapper(mapper);
}
}
然而它以格式序列化日期yyyy-MM-dd
,甚至没有时间信息!
在你问之前:
- 泽西版:
2.32
- 杰克逊版:
2.7.2
- 是的,我知道这些版本有点旧,这是我正在尝试升级的遗留项目。我开始时它仍在使用一些 SOAP 端点,甚至没有使用 Maven 进行依赖...
- 是的,我在提问之前搜索了其他问题,我尝试了他们的解决方案,但无济于事。
- 是的,我
pom.xml
的配置正确。 - 是的,我的依赖是正确的,没有遗漏或错误,因为一切都在工作,除了日期序列化......
- 是的,我设置了一个 JUnit 测试以查看我
ObjectMapper
的设置是否正确,并且它是预期格式的读取和写入日期。 - 是的,我知道这
WRITE_DATES_AS_TIMESTAMP
一点,它不是,我试着评论那条线。 - 是的,我进行了测试以保证泽西岛实际上正在使用该提供商。
- 是的,其他
ObjectMapper
配置按预期工作,只有DateFormat
杰克逊似乎忽略了。 - 是的,我尝试使用其他格式,杰克逊也忽略了它们。
- 是的,我尝试了
@JsonFormat
注释,它可以工作,但是该注释是设置特定于字段的格式,我想要的是杰克逊使用的全球日期格式。 - 是的,我尝试使用 a
ContextResolver<ObjectMapper>
并且它也不起作用。 - 是的,我尝试了一个快速修复来使用 Gson 而不是 Jackson 进行测试,它就像一个魅力,但项目的所有其他部分已经在使用 Jackson,切换会......很耗时。
- 是的,我知道有多种方法可以实现...“解决”这个问题,但我不想“解决”它,我想修复它。
请记住:我的目标是为杰克逊设置一个全局日期格式,以便以干净、简单的方式用于序列化/反序列化
欢迎任何帮助。
更新
根据要求,这里有更多代码:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- package info here -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<jersey.version>2.32</jersey.version>
<jackson.version>2.7.2</jackson.version>
</properties>
<repositories>
<repository>
<id>mavenCentral</id>
<url>https://repo1.maven.org/maven2/</url>
</repository>
</repositories>
<build>
<sourceDirectory>src/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<attachClasses>true</attachClasses>
<webXml>src/web/WEB-INF/web.xml</webXml>
<webResources>
<resource>
<directory>src/web</directory>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-multipart -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.containers/jersey-container-servlet -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</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.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>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.18</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<!-- Enable SOAP (because it still has some leftover SOAP endpoints) -->
<!-- API -->
<dependency>
<groupId>jakarta.xml.ws</groupId>
<artifactId>jakarta.xml.ws-api</artifactId>
<version>2.3.3</version>
</dependency>
<!-- Runtime -->
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.3.3</version>
</dependency>
</dependencies>
</project>
Application.java
@ApplicationPath("/")
public class Application extends ResourceConfig {
public Application() {
// Register Jackson provider
register(JacksonProvider.class);
// Register endpoint
register(ExampleEndpoint.class);
// More registrations here...
}
}
ExampleEndpoint.java
@Path("/example")
public class ExampleEndpoint {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getTimestamp() {
try {
Map<String, Object> response = new HashMap<>();
response.put("current_timestamp", new Date());
return Response.ok(response).build();
} catch (Exception e) {
return Response.serverError().entity(e).build();
}
}
}
解决方案
由于日期格式在测试中按预期工作,但在部署应用程序时却没有,因此您需要验证在部署应用程序时使用的实际提供程序版本。
我猜(直到您提供一个最小的可重现示例,即您的类路径中有两个提供程序 jar,一个由您的应用程序服务器提供(即在 Glassfish 模块目录中),一个打包在您的应用程序中(或在 lib/ext 目录中)。这可以解释您评论中所述的奇怪行为:
情节变厚了:我实际上只是重新部署了应用程序,甚至没有更改任何一行代码,它就开始工作了。然后(再次没有任何更改)我再次重新部署代码,它停止工作。
可能是在部署之间更改了类路径顺序,并且您的应用程序使用了其他提供程序版本。
如果是这种情况,一种工作方法是:
- 验证您的应用服务器提供的版本
- 在 pom.xml 中使用具有
provided
范围的确切版本(因此您将始终使用 AS 提供的实现
注意:这种方法会将您的应用绑定到 AS。
推荐阅读
- firebase - Firebase Cloud Functions:`convert` 因 ChildProcessError 而失败
- reactjs - 如何将 react-helmet 与 netlify 预渲染一起使用?
- python - 如何使用 atom-python-run 包在 Atom 中运行 python 脚本?
- git - 旧的提交出现在新的拉取请求中?
- java - 无法在线性布局中移动项目
- jquery - 在更新的路线上显示移动标记 (gmaps.js)
- java - IntelliJ IDEA 添加 JDK 10:“所选目录不是 JDK 的有效主目录”
- php - 仪表板索引页四个错误,但本地主机工作良好
- python - 如何从 python 脚本打开 .cmd 文件
- mysql - SQL - 多个表 - 始终返回数据或 null