Hibernate-生成JSON时覆盖日期的输出格式

问题描述 投票:0回答:1

[在我的实体中定义了一个时态:

  @Temporal(TemporalType.TIMESTAMP)
  @Column(name = "start_time", length = 19, nullable = false)
  public Date getStartTime() {
    return this.startTime;
  }

  public void setStartTime(Date startTime) {
    this.startTime = startTime;
  }

然后将JSON编组为这样(简化):

  @GET
  @RestSecure
  @Path("/list")
  @Produces(MediaType.APPLICATION_JSON)
  public Response list(){
    return Response.status(Response.Status.OK).entity(myEntityList).build();
  }

是否有覆盖输出日期格式的简单方法?

我得到的是这样的时代:

"startTime": 1582261711000,

我需要的是ISO 8601格式的日期,如下所示:

"startTime": "2020-02-21T05:08:31Z",
hibernate nhibernate-mapping
1个回答
1
投票

您可以使用杰克逊的DateFormat批注:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssZ")
© www.soinside.com 2019 - 2024. All rights reserved.