@JsonFormat處理LocalDateTime失效的問題
@JsonFormat處理LocalDateTime失效
Failed to convert property value of type ‘java.lang.String’ to required type ‘localdatetime’ for property ‘time’ xxxx
Api 請(qǐng)求參數(shù)中,通過需要用時(shí)間LocalDateTime,希望通過@JsonFormat() 處理時(shí)間格式:
@GetMapping("/user") public UserDTO getUser(UserDTO name) { ?? ?xxx } @Data public class UserDTO { ? ? @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") ? ? LocalDateTime time; }
當(dāng)我們通過get請(qǐng)求,通過表單的方式提交時(shí),就會(huì)報(bào)上面按個(gè)轉(zhuǎn)換異常參數(shù);
解決:
方案一:改成請(qǐng)求體的方式提交,@RequestBody
? ? //get請(qǐng)求 ? ? @GetMapping("/user") ? ? public UserDTO getUser(@RequestBody UserDTO name) { ? ? } ? ? // post 請(qǐng)求 ? ? @PostMapping("/user") ? ? public UserDTO getUser(@RequestBody UserDTO name) { ? ? }
方案二:同時(shí)添加@DateTimeFormat()注解這個(gè)是Spring提供的注解
@Data public class UserDTO { ? ? @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") ? ? @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") ? ? LocalDateTime time; }
@DateTimeFormat
用于將請(qǐng)求參數(shù)序列化@JsonFormat()
將返回參數(shù)序列話
@JsonFormat格式化LocalDateTime失敗
我們可以使用SpringBoot依賴中的@JsonFormat注解,將前端通過json傳上來的時(shí)間,通過@RequestBody自動(dòng)綁定到Bean里的LocalDateTime成員上。具體的綁定注解使用方法如下所示。
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
出現(xiàn)問題的版本
我使用Spring Boot 2.0.0 時(shí),直接在字段上加上@JsonFormat 注解就可以完成數(shù)據(jù)的綁定。
而在使用Spring Boot 1.5.8時(shí),只在字段上加上@JsonFormat 注解,在數(shù)據(jù)綁定時(shí)無法將Date類型的數(shù)據(jù)自動(dòng)轉(zhuǎn)化為字符串類型的數(shù)據(jù)。
解決:
1.將SpringBoot版本升級(jí)為2.0.0及以上。
2.如果不升級(jí)SpringBoot版本,可以按照下面的方式解決問題。
不升級(jí)SpringBoot版本,添加Jackson對(duì)Java Time的支持后,就能解決這個(gè)問題。
在pom.xml中添加:
<dependency> ? ? <groupId>com.fasterxml.jackson.module</groupId> ? ? <artifactId>jackson-module-parameter-names</artifactId> </dependency> <dependency> ? ? <groupId>com.fasterxml.jackson.datatype</groupId> ? ? <artifactId>jackson-datatype-jdk8</artifactId> </dependency> <dependency> ? ? <groupId>com.fasterxml.jackson.datatype</groupId> ? ? <artifactId>jackson-datatype-jsr310</artifactId> </dependency>
添加JavaConfig,自動(dòng)掃描新添加的模塊:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.fasterxml.jackson.databind.ObjectMapper; ? @Configuration public class JacksonConfig { ? ? ? @Bean ? ? public ObjectMapper serializingObjectMapper() { ? ? ? ? ObjectMapper objectMapper = new ObjectMapper(); ? ? ? ? objectMapper.findAndRegisterModules(); ? ? ? ? return objectMapper; ? ? } }
或者在application.properties添加如下配置:
spring.jackson.serialization.write-dates-as-timestamps=false
或者只注冊(cè)JavaTimeModule,添加下面的Bean
@Bean public ObjectMapper serializingObjectMapper() { ? ObjectMapper objectMapper = new ObjectMapper(); ? objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); ? objectMapper.registerModule(new JavaTimeModule()); ? return objectMapper; }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring @Cacheable redis異常不影響正常業(yè)務(wù)方案
這篇文章主要介紹了Spring @Cacheable redis異常不影響正常業(yè)務(wù)方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Springboot+AOP實(shí)現(xiàn)返回?cái)?shù)據(jù)提示語國際化的示例代碼
這篇文章主要介紹了Springboot+AOP實(shí)現(xiàn)返回?cái)?shù)據(jù)提示語國際化的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07Java線程池ThreadPoolExecutor的使用及其原理詳細(xì)解讀
這篇文章主要介紹了Java線程池ThreadPoolExecutor的使用及其原理詳細(xì)解讀,線程池是一種多線程處理形式,處理過程中將任務(wù)添加到隊(duì)列,然后在創(chuàng)建線程后自動(dòng)啟動(dòng)這些任務(wù),線程池線程都是后臺(tái)線程,需要的朋友可以參考下2023-12-12Springboot2.x 使用 Log4j2 異步打印日志的實(shí)現(xiàn)
這篇文章主要介紹了Springboot2.x 使用 Log4j2 異步打印日志的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12解決RestTemplate反序列化嵌套對(duì)象的問題
這篇文章主要介紹了解決RestTemplate反序列化嵌套對(duì)象的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11