簡體   English   中英

如何更改 ResponseEntity 的時間戳字段格式?

[英]How to change timestamp field format for ResponseEntity?

我的 API 使用org.springframework.http.ResponseEntity返回響應正文

我需要將時間戳字段格式更改為特定格式。

我的 application.properties 文件中要使用的屬性是什么?

我試圖在 web 上找到它,但我找到了其他 3rd 方庫的參考資料,而不是 spring 的參考資料。

如果我還可以以其他格式定義message字段,我會很棒。

當前響應正文:

{
  "timestamp": "Oct 2, 2019 3:24:32 PM",
  "status": 200,
  "error": "OK",
  "message": "Initialization failed. cfgId doesn't exist",
  "path": "/a/b/c/d/init"
}

我沒有使用任何第三方庫來返回 json,它只是org.springframework.http.ResponseEntity

我正在尋找一個通用的解決方案,而不是每個領域。 我想要一個 application.properties 值來修復它。

來自Spring Docs on Common application properties

如果您使用像 Jackson 這樣的第三方,則必須設置spring.jackson.data-format屬性:

在此處輸入圖像描述

如果您不使用,則必須設置spring.mvc.date-format屬性

在此處輸入圖像描述

現在,您已經提到了格式yyyy-MM-dd'T'HH:mm:ss ,因此您必須設置任何屬性

spring.jackson.data-format=yyyy-MM-dd'T'HH:mm:ss
spring.mvc.date-format=yyyy-MM-dd'T'HH:mm:ss

希望這對您有所幫助。

謝謝。

檢查文檔以獲取更多信息。 :)

您還可以在 Jackson 中使用 @JsonFormat

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
public Date getCurrentDate() {
    return new Date();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM