在我的实体中定义了一个时间,例如:

  @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",

最佳答案

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

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssZ")

10-06 09:11
查看更多