springboot:接收date類型的參數(shù)方式
springboot:接收date類型的參數(shù)
今天有個postmapping方法,地址都正確,就是死活進(jìn)不去,真是奇怪了。
終于從日志中得出些端倪,見下:
只有這個屬性報錯,恰恰這個屬性是Date型。
這句話說得更清楚:
"defaultMessage":"Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'expireTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.alibaba.fastjson.annotation.JSONField java.util.Date] for value '2018-06-29'; nested exception is java.lang.IllegalArgumentException",
查找資料,說只要在字段上加上注解:@DateTimeFormat(pattern="yyyy-MM-dd")
加上后就一切OK了。
springboot 傳遞Date等實體參數(shù)時候報錯
傳遞參數(shù)Date時候報錯:
"exception": "org.springframework.web.method.annotation.MethodArgumentTypeMismatchException",
"message": "Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam java.util.Date] for value '2016-12-27 09:44:58'; nested exception is java.lang.IllegalArgumentException",
swagger2:
@ApiImplicitParam(name = "startDate", paramType = "query", value = "生效時間", dataType = "Date"),
@ApiImplicitParam(name = "endDate", paramType = "query", value = "失效時間", dataType = "Date"),
params由:
@RequestParam(value = "startDate", required = false) Date startDate, @RequestParam(value = "endDate", required = false) Date endDate,
改為:
@ModelAttribute Date startDate, @ModelAttribute Date endDate,
此時 參數(shù)傳遞正常 但是date值都存在切為當(dāng)前時間
改回
@RequestParam(value = "startDate", required = false) Date startDate, @RequestParam(value = "endDate", required = false) Date endDate,
并加入
@InitBinder protected void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true)); }
此時參數(shù)傳遞正常
時間段查詢條件
if (startDate!=null) {//開始時間 if(endDate!=null){//結(jié)束時間 結(jié)束時間部位空 查詢時間段內(nèi)數(shù)據(jù) predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("endDate").as(Date.class), startDate ));//輸入開始時間>=開始生效時間 predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("startDate").as(Date.class), endDate ));//輸入結(jié)束時間<=失效時間 }else{ predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("startDate").as(Date.class), startDate )); predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("endDate").as(Date.class), startDate )); } }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java數(shù)據(jù)機(jī)構(gòu)中關(guān)于并查集的詳解
并查集是一種樹型的數(shù)據(jù)結(jié)構(gòu),用于處理一些不相交集合的合并及查詢問題,如果你還不了解并查集,請看接下來的文章,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值2021-09-09淺談springMVC接收前端json數(shù)據(jù)的總結(jié)
下面小編就為大家分享一篇淺談springMVC接收前端json數(shù)據(jù)的總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03Springboot集成阿里云OSS上傳文件系統(tǒng)教程
這篇文章主要介紹了Springboot集成阿里云OSS上傳文件系統(tǒng)教程,通過詳細(xì)的圖文展示,代碼步驟的展示和文件配置信息,希望對你有所幫助2021-06-06SpringCloud中的分布式鎖用法示例詳解(Java+Redis SETNX命令)
在Spring Cloud項目中,使用Java和Redis結(jié)合實現(xiàn)的分布式鎖可以確保訂單的一致性和并發(fā)控制,分布式鎖的使用能夠在多個實例同時提交訂單時,僅有一個實例可以成功進(jìn)行操作,本文給大家介紹Spring,Cloud中的分布式鎖用法詳解(Java+Redis SETNX命令),感興趣的朋友一起看看吧2023-10-10java sleep()和wait()的區(qū)別點總結(jié)
在本篇文章里小編給大家整理了一篇關(guān)于java sleep()和wait()的區(qū)別的相關(guān)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-04-04JDK21新特性Record?Patterns記錄模式詳解(最新推薦)
這篇文章主要介紹了JDK21新特性Record?Patterns記錄模式詳解,本JEP建立在Pattern?Matching?for?instanceof(JEP?394)的基礎(chǔ)上,該功能已在JDK?16中發(fā)布,它與Pattern?Matching?for?switch(JEP?441)共同演進(jìn),需要的朋友可以參考下2023-09-09