Java.util.Date在数字格式中的问题是resteasy客户端。

huangapple go评论78阅读模式
英文:

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

问题

以下是您提供的内容的中文翻译:

这是一个 resteasy REST API 的示例。

pom.xml 的一部分:

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

    <!-- Jersey DI 和核心依赖 -->
    <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>

    <!-- 添加 Jackson 作为 JSON 提供程序 -->
    <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>

以及我的资源类的一部分:

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

因此,当我请求 http://localhost:8080/test 时,响应是 1685343320152 而不是日期格式。

哪里出错了?

英文:

There is a sample of resteasy rest api.

Part of pom.xml:

&lt;dependencies&gt;
   &lt;dependency&gt;
        &lt;groupId&gt;org.glassfish.jersey.containers&lt;/groupId&gt;
        &lt;artifactId&gt;jersey-container-grizzly2-http&lt;/artifactId&gt;
    &lt;/dependency&gt;

    &lt;!-- Jersey DI and core--&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.glassfish.jersey.inject&lt;/groupId&gt;
        &lt;artifactId&gt;jersey-hk2&lt;/artifactId&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.glassfish.hk2&lt;/groupId&gt;
        &lt;artifactId&gt;hk2-metadata-generator&lt;/artifactId&gt;
        &lt;version&gt;3.0.3&lt;/version&gt;
    &lt;/dependency&gt;

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

and the part of my resource class:

@Path(&quot;/test&quot;)
@GET
@Produces(&quot;application/json&quot;)
@Consumes(&quot;application/json&quot;)
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?

答案1

得分: 1

默认是从纪元开始计数

显然,Jackson默认将java.util.Date对象序列化为自1970年第一时刻的纪元参考以来的毫秒数,以UTC时间(1970-01-01T00:00Z)表示。

ISO 8601

最好序列化为ISO 8601格式。使用ObjectMapper将格式从默认格式更改为ISO 8601。

// 来源代码:https://www.baeldung.com/jackson-serialize-dates
ObjectMapper mapper = new ObjectMapper();
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.setDateFormat(new StdDateFormat().withColonInTimeZone(true));

请参阅《Jackson日期》(baeldung)。

java.time

您正在使用多年前被现代java.time类(JSR 310定义)取代的存在严重缺陷的日期时间类。

具体来说,java.util.Date已被Instant取代。两者都表示从UTC的零小时-分-秒偏移量中看到的时刻,但分辨率分别为毫秒和纳秒。

我强烈建议您考虑用现代类替换旧的类。

英文:

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.

huangapple
  • 本文由 发表于 2023年5月29日 15:00:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355259.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定