首页 > 解决方案 > 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,甚至没有时间信息!

在你问之前:

请记住:我的目标是为杰克逊设置一个全局日期格式,以便以干净、简单的方式用于序列化/反序列化

欢迎任何帮助。

更新

根据要求,这里有更多代码:

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

标签: javarestjacksonjerseyjax-rs

解决方案


由于日期格式在测试中按预期工作,但在部署应用程序时却没有,因此您需要验证在部署应用程序时使用的实际提供程序版本。

我猜(直到您提供一个最小的可重现示例,即您的类路径中有两个提供程序 jar,一个由您的应用程序服务器提供(即在 Glassfish 模块目录中),一个打包在您的应用程序中(或在 lib/ext 目录中)。这可以解释您评论中所述的奇怪行为:

情节变厚了:我实际上只是重新部署了应用程序,甚至没有更改任何一行代码,它就开始工作了。然后(再次没有任何更改)我再次重新部署代码,它停止工作。

可能是在部署之间更改了类路径顺序,并且您的应用程序使用了其他提供程序版本。

如果是这种情况,一种工作方法是:

  1. 验证您的应用服务器提供的版本
  2. 在 pom.xml 中使用具有provided范围的确切版本(因此您将始终使用 AS 提供的实现

注意:这种方法会将您的应用绑定到 AS。


推荐阅读