英文:
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:
<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?
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论