springboot:接收date類型的參數方式
springboot:接收date類型的參數
今天有個postmapping方法,地址都正確,就是死活進不去,真是奇怪了。
終于從日志中得出些端倪,見下:
只有這個屬性報錯,恰恰這個屬性是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等實體參數時候報錯
傳遞參數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,
此時 參數傳遞正常 但是date值都存在切為當前時間
改回
@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));
}
此時參數傳遞正常
時間段查詢條件
if (startDate!=null) {//開始時間
if(endDate!=null){//結束時間 結束時間部位空 查詢時間段內數據
predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("endDate").as(Date.class), startDate ));//輸入開始時間>=開始生效時間
predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("startDate").as(Date.class), endDate ));//輸入結束時間<=失效時間
}else{
predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("startDate").as(Date.class), startDate ));
predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("endDate").as(Date.class), startDate ));
}
}
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
自定義Jackson的ObjectMapper如何實現@ResponseBody的自定義渲染
這篇文章主要介紹了自定義Jackson的ObjectMapper如何實現@ResponseBody的自定義渲染,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
關于Spring的@Autowired依賴注入常見錯誤的總結
有時我們會使用@Autowired自動注入,同時也存在注入到集合、數組等復雜類型的場景。這都是方便寫 bug 的場景,本篇文章帶你了解Spring @Autowired依賴注入的坑2021-09-09
FastJson踩坑:@JsonField在反序列化時失效的解決
這篇文章主要介紹了FastJson踩坑:@JsonField在反序列化時失效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
解決idea默認帶的equals和hashcode引起的bug
這篇文章主要介紹了解決idea默認帶的equals和hashcode引起的bug,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07



