springboot:接收date類(lèi)型的參數(shù)方式
springboot:接收date類(lèi)型的參數(shù)
今天有個(gè)postmapping方法,地址都正確,就是死活進(jìn)不去,真是奇怪了。
終于從日志中得出些端倪,見(jiàn)下:
只有這個(gè)屬性報(bào)錯(cuò),恰恰這個(gè)屬性是Date型。
這句話(huà)說(shuō)得更清楚:
"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",
查找資料,說(shuō)只要在字段上加上注解:@DateTimeFormat(pattern="yyyy-MM-dd")
加上后就一切OK了。
springboot 傳遞Date等實(shí)體參數(shù)時(shí)候報(bào)錯(cuò)
傳遞參數(shù)Date時(shí)候報(bào)錯(cuò):
"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 = "生效時(shí)間", dataType = "Date"),
@ApiImplicitParam(name = "endDate", paramType = "query", value = "失效時(shí)間", dataType = "Date"),
params由:
@RequestParam(value = "startDate", required = false) Date startDate, @RequestParam(value = "endDate", required = false) Date endDate,
改為:
@ModelAttribute Date startDate, @ModelAttribute Date endDate,
此時(shí) 參數(shù)傳遞正常 但是date值都存在切為當(dāng)前時(shí)間
改回
@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í)參數(shù)傳遞正常
時(shí)間段查詢(xún)條件
if (startDate!=null) {//開(kāi)始時(shí)間 if(endDate!=null){//結(jié)束時(shí)間 結(jié)束時(shí)間部位空 查詢(xún)時(shí)間段內(nèi)數(shù)據(jù) predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("endDate").as(Date.class), startDate ));//輸入開(kāi)始時(shí)間>=開(kāi)始生效時(shí)間 predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("startDate").as(Date.class), endDate ));//輸入結(jié)束時(shí)間<=失效時(shí)間 }else{ predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("startDate").as(Date.class), startDate )); predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("endDate").as(Date.class), startDate )); } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java數(shù)據(jù)機(jī)構(gòu)中關(guān)于并查集的詳解
并查集是一種樹(shù)型的數(shù)據(jù)結(jié)構(gòu),用于處理一些不相交集合的合并及查詢(xún)問(wèn)題,如果你還不了解并查集,請(qǐng)看接下來(lái)的文章,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值2021-09-09淺談springMVC接收前端json數(shù)據(jù)的總結(jié)
下面小編就為大家分享一篇淺談springMVC接收前端json數(shù)據(jù)的總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03Springboot集成阿里云OSS上傳文件系統(tǒng)教程
這篇文章主要介紹了Springboot集成阿里云OSS上傳文件系統(tǒng)教程,通過(guò)詳細(xì)的圖文展示,代碼步驟的展示和文件配置信息,希望對(duì)你有所幫助2021-06-06SpringCloud中的分布式鎖用法示例詳解(Java+Redis SETNX命令)
在Spring Cloud項(xiàng)目中,使用Java和Redis結(jié)合實(shí)現(xiàn)的分布式鎖可以確保訂單的一致性和并發(fā)控制,分布式鎖的使用能夠在多個(gè)實(shí)例同時(shí)提交訂單時(shí),僅有一個(gè)實(shí)例可以成功進(jìn)行操作,本文給大家介紹Spring,Cloud中的分布式鎖用法詳解(Java+Redis SETNX命令),感興趣的朋友一起看看吧2023-10-10java sleep()和wait()的區(qū)別點(diǎn)總結(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