【问题标题】:Hibernate - Override output format of date when generating JSONHibernate - 生成 JSON 时覆盖日期的输出格式
【发布时间】:2020-03-13 21:36:24
【问题描述】:

我的实体中的时间定义如下:

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

【问题讨论】:

    标签: java json hibernate jpa jackson


    【解决方案1】:

    可以使用jackson的DateFormat注解:

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

    【讨论】:

    • 太棒了,我知道一定有一种简单的方法可以做到这一点。谢谢!!!
    猜你喜欢
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多