![](/img/trans.png)
[英]@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = “MM/dd/yyyy HH:mm”) adding Year to date
[英]@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") not working as expected
我的應用程序無法格式化日期輸入。 我正在使用 com.fasterxml.jackson.annotation.JsonFormat
JSON 輸入:
"startDate": "2019-08-07"
在我的類中定義的成員變量:
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private LocalDate startDate;
依賴項:
compile('com.fasterxml.jackson.core:jackson-annotations:2.10.0')
compile('com.fasterxml.jackson.core:jackson-databind:2.10.0')
compile('com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.10.0')
看到的異常:
Field error in object 'searchCriteriaDTO' on field 'startDate': rejected value [2019-08-07]; codes [typeMismatch.searchCriteriaDTO.startDate,typeMismatch.startDate,typeMismatch.java.time.LocalDate,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [searchCriteriaDTO.startDate,startDate]; arguments []; default message [startDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDate' for property 'startDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.fasterxml.jackson.annotation.JsonFormat java.time.LocalDate] for value '2019-08-07'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2019-08-07]]
有沒有人遇到過這個問題? 任何幫助表示贊賞。
您可能忘記向 ObjectMapper 注冊 JavaTimeModule 了嗎?
om.registerModule(new JavaTimeModule());
我不知道確切的錯誤消息,但是使用 LocalDate 和 JavaTimeModule 去/序列化對我來說沒問題; 也許 spring-boot 不會自動注冊 Jackson Databind 模塊。
而且,順便說一句,我認為不需要注釋,因為 ISO 日期格式是默認格式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.