springboot后端使用LocalDate接收日期的問題解決
報錯內(nèi)容
Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDate' for property 'xxxx';
nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.LocalDate] for value '2021-10-03';
nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2021-10-03]
當后端的實體類 使用jdk8的時間時,會報以上的問題
這個時候就需要使用 @DateTimeFormat(pattern = “yyyy-MM-dd”) 注解
該注解中的pattern 要和前端傳過來的日期格式保持一致
如果是 LocalDateTime 可以使用 @DateTimeFormat(pattern = “yyyy-MM-dd HH:mm:ss”)
這是反序列化時的做法,當序列化時 也就是后端傳給前端時,轉(zhuǎn)換時間格式使用 @JsonFormat(pattern = “yyyy-MM-dd”, timezone = “GMT+8”) 注解
@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
private LocalDate beginTime;也可以設置全局
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
// 全局配置序列化返回 JSON 處理
SimpleModule simpleModule = new SimpleModule();
//封裝類型 Long ==> String
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
simpleModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
simpleModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
simpleModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN)));
simpleModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN)));
simpleModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN)));
objectMapper.registerModule(simpleModule);
objectMapper.setDateFormat(new SimpleDateFormat(DatePattern.NORM_DATETIME_PATTERN));
objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
return objectMapper;
}到此這篇關于springboot后端使用LocalDate接收日期的問題解決的文章就介紹到這了,更多相關springboot LocalDate接收日期內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot優(yōu)化啟動速度實現(xiàn)過程
本文主要介紹了SpringBoot啟動慢的原因,并提供了多種針對性優(yōu)化方案,如延遲Bean初始化、關閉JMX、減少組件掃描、升級JDK及SpringBoot版本等,旨在提升應用啟動速度2026-05-05
Spring的@Validation和javax包下的@Valid區(qū)別以及自定義校驗注解
這篇文章主要介紹了Spring的@Validation和javax包下的@Valid區(qū)別以及自定義校驗注解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01
Feign調(diào)用服務時丟失Cookie和Header信息的解決方案
這篇文章主要介紹了Feign調(diào)用服務時丟失Cookie和Header信息的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
springsecurity中http.permitall與web.ignoring的區(qū)別說明
這篇文章主要介紹了springsecurity中http.permitall與web.ignoring的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
Java如何實現(xiàn)數(shù)據(jù)壓縮所有方式性能測試
本文介紹了多種壓縮算法及其在Java中的實現(xiàn),包括LZ4、BZip2、Deflate、Gzip和7z等,LZ4以其高效的壓縮和解壓縮速度而受到青睞,特別是在大數(shù)據(jù)處理場景中,通過對比不同壓縮算法的性能和壓縮率,我們選擇了最適合當前項目需求的壓縮工具2025-02-02

