Search code examples
javajsondateglassfishresteasy

Problem of Java.util.Date in numeric format is resteasy client


There is a sample of resteasy rest api.

Part of pom.xml:

<dependencies>
   <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-grizzly2-http</artifactId>
    </dependency>

    <!-- Jersey DI and core-->
    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.glassfish.hk2</groupId>
        <artifactId>hk2-metadata-generator</artifactId>
        <version>3.0.3</version>
    </dependency>

    <!-- add jackson as json provider -->
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson-provider</artifactId>
        <version>2.3.10.Final</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-client</artifactId>
        <version>3.9.3.Final</version>
    </dependency>
</dependencies>

and the part of my resource class:

@Path("/test")
@GET
@Produces("application/json")
@Consumes("application/json")
public Date test() {
    return new Date();
}

So when I request http://localhost:8080/test, the response is 1685343320152 instead of date format.

Where is wrong?


Solution

  • I can address part of your Question, explaining why the response is 1685343320152.

    Default is count from epoch

    Apparently, Jackson defaults to serializing a java.util.Date object to a count of milliseconds since the epoch reference of first moment of 1970 as seen in UTC (1970-01-01T00:00Z).

    ISO 8601

    Better to serialize to ISO 8601 format. Use ObjectMapper to change the format from that default to ISO 8601.

    // Source code from: https://www.baeldung.com/jackson-serialize-dates
    ObjectMapper mapper = new ObjectMapper() ;
    mapper.disable( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS ) ;
    mapper.setDateFormat( new StdDateFormat().withColonInTimeZone( true ) ) ;
    

    See Jackson Date by baeldung.

    java.time

    You are using terribly flawed date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310.

    Specifically, java.util.Date was replaced by Instant. Both represent a moment as seen with an offset of zero hours-minutes-seconds from UTC, but with a resolution of milliseconds versus nanoseconds respectively.

    I strongly suggest you consider replacing your use of the legacy classes with the modern ones.