解決springboot 實(shí)體類(lèi)String轉(zhuǎn)Date類(lèi)型的坑
springboot 實(shí)體類(lèi)String轉(zhuǎn)Date類(lèi)型
前端傳入一個(gè)String的時(shí)間字符串如:2019-07-18 23:59:59
后端實(shí)體類(lèi)要在頭頂加注解:
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")

不然會(huì)出現(xiàn)報(bào)錯(cuò)

Date解析String類(lèi)型的參數(shù)
1.首先建立String to Date 的解析實(shí)現(xiàn)
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.converter.Converter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StringToDateConverter implements Converter<String, Date> {
private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
private static final String shortDateFormat = "yyyy-MM-dd";
@Override
public Date convert(String value) {
if (StringUtils.isEmpty(value)) {
return null;
}
value = value.trim();
try {
if (value.contains("-")) {
SimpleDateFormat formatter;
if (value.contains(":")) {
formatter = new SimpleDateFormat(dateFormat);
} else {
formatter = new SimpleDateFormat(shortDateFormat);
}
Date dtDate = formatter.parse(value);
return dtDate;
} else if (value.matches("^\\d+$")) {
Long lDate = new Long(value);
return new Date(lDate);
}
} catch (Exception e) {
throw new RuntimeException(String.format("parser %s to Date failed", value));
}
throw new RuntimeException(String.format("parser %s to Date failed", value));
}
}
2.創(chuàng)建全局的解析配置
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import javax.annotation.PostConstruct;
@Configuration
public class DateHandlerAdapter {
@Autowired
private RequestMappingHandlerAdapter handlerAdapter;
/**
* 增加字符串轉(zhuǎn)日期的全局適配器
*/
@PostConstruct
public void initEditableAvlidation() {
ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter
.getWebBindingInitializer();
if (initializer.getConversionService() != null) {
GenericConversionService genericConversionService = (GenericConversionService) initializer
.getConversionService();
genericConversionService.addConverter(new StringToDateConverter());
}
}
}
添加完這兩個(gè)文件以后 在傳參數(shù)類(lèi)型為Date的參數(shù)時(shí)就不會(huì)再報(bào) date解析失敗的錯(cuò)誤了。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot中捕獲異常錯(cuò)誤信息并將其保存到數(shù)據(jù)庫(kù)中的操作方法
這篇文章主要介紹了Spring Boot中捕獲異常錯(cuò)誤信息并將其保存到數(shù)據(jù)庫(kù)中的操作方法,通過(guò)實(shí)例代碼介紹了使用Spring Data JPA創(chuàng)建一個(gè)異常信息的存儲(chǔ)庫(kù)接口,以便將異常信息保存到數(shù)據(jù)庫(kù),需要的朋友可以參考下2023-10-10
Java?HashMap中除了死循環(huán)之外的那些問(wèn)題
這篇文章主要介紹了Java?HashMap中除了死循環(huán)之外的那些問(wèn)題,這些問(wèn)題大致可以分為兩類(lèi),程序問(wèn)題和業(yè)務(wù)問(wèn)題,下面文章我們一個(gè)一個(gè)來(lái)看,需要的小伙伴可以參考一下2022-05-05
Idea調(diào)用WebService的關(guān)鍵步驟和注意事項(xiàng)
這篇文章主要介紹了如何在Idea中調(diào)用WebService,包括理解WebService的基本概念、獲取WSDL文件、閱讀和理解WSDL文件、選擇對(duì)接測(cè)試工具或方式、發(fā)送請(qǐng)求和接收響應(yīng)、處理響應(yīng)結(jié)果以及錯(cuò)誤處理,需要的朋友可以參考下2025-01-01
Java實(shí)現(xiàn)時(shí)間與字符串互相轉(zhuǎn)換詳解
這篇文章主要為大家詳細(xì)介紹了Java中實(shí)現(xiàn)時(shí)間與字符串互相轉(zhuǎn)換的相關(guān)方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04
Java通過(guò)IO流輸出文件目錄的實(shí)例代碼
這篇文章主要介紹了Java通過(guò)IO流輸出文件目錄,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
Spring @Configuration和@Component的區(qū)別
今天小編就為大家分享一篇關(guān)于Spring @Configuration和@Component的區(qū)別,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
Java中MyBatis的動(dòng)態(tài)語(yǔ)句詳解
這篇文章主要介紹了Java中MyBatis的動(dòng)態(tài)語(yǔ)句詳解,動(dòng)態(tài) SQL 是 MyBatis 的強(qiáng)大特性之一,通過(guò)不同參數(shù)生成不同的 SQL,可以動(dòng)態(tài)地對(duì)數(shù)據(jù)持久層進(jìn)行操作,而不需要每個(gè)數(shù)據(jù)訪問(wèn)操作都要進(jìn)行手動(dòng)地拼接 SQL 語(yǔ)句,需要的朋友可以參考下2023-08-08

