springboot:接收date類型的參數(shù)方式
springboot:接收date類型的參數(shù)
今天有個postmapping方法,地址都正確,就是死活進(jìn)不去,真是奇怪了。
終于從日志中得出些端倪,見下:
只有這個屬性報(bào)錯,恰恰這個屬性是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ù)時(shí)候報(bào)錯
傳遞參數(shù)Date時(shí)候報(bào)錯:
"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í)間段查詢條件
if (startDate!=null) {//開始時(shí)間
if(endDate!=null){//結(jié)束時(shí)間 結(jié)束時(shí)間部位空 查詢時(shí)間段內(nèi)數(shù)據(jù)
predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("endDate").as(Date.class), startDate ));//輸入開始時(shí)間>=開始生效時(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 ));
}
}
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?@Conditional注解示例詳細(xì)講解
@Conditional是Spring4新提供的注解,它的作用是按照一定的條件進(jìn)行判斷,滿足條件給容器注冊bean,這篇文章主要介紹了Spring?@Conditional注解示例詳細(xì)講解,需要的朋友可以參考下2022-11-11
自定義Jackson的ObjectMapper如何實(shí)現(xiàn)@ResponseBody的自定義渲染
這篇文章主要介紹了自定義Jackson的ObjectMapper如何實(shí)現(xiàn)@ResponseBody的自定義渲染,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
關(guān)于Spring的@Autowired依賴注入常見錯誤的總結(jié)
有時(shí)我們會使用@Autowired自動注入,同時(shí)也存在注入到集合、數(shù)組等復(fù)雜類型的場景。這都是方便寫 bug 的場景,本篇文章帶你了解Spring @Autowired依賴注入的坑2021-09-09
FastJson踩坑:@JsonField在反序列化時(shí)失效的解決
這篇文章主要介紹了FastJson踩坑:@JsonField在反序列化時(shí)失效的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Springboot分模塊項(xiàng)目搭建的實(shí)現(xiàn)
在軟件開發(fā)中,利用Spring?Boot進(jìn)行分模塊項(xiàng)目搭建能夠提高代碼的模塊化和復(fù)用性,本文主要介紹了Springboot分模塊項(xiàng)目搭建的實(shí)現(xiàn),感興趣的可以了解一下2024-10-10
解決idea默認(rèn)帶的equals和hashcode引起的bug
這篇文章主要介紹了解決idea默認(rèn)帶的equals和hashcode引起的bug,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07



