springboot:接收date类型的参数方式
目录
- springboot:接收date类型的参数
- 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",params由:
"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 = "https://www.it610.com/article/生效时间", dataType = "Date"),
@ApiImplicitParam(name = "endDate", paramType = "query", value = "https://www.it610.com/article/失效时间", dataType = "Date"),
@RequestParam(value = "https://www.it610.com/article/startDate", required = false) Date startDate,@RequestParam(value = "https://www.it610.com/article/endDate", required = false) Date endDate,
改为:
@ModelAttribute Date startDate,@ModelAttribute Date endDate,
此时 参数传递正常 但是date值都存在切为当前时间
改回
@RequestParam(value = "https://www.it610.com/article/startDate", required = false) Date startDate,@RequestParam(value = "https://www.it610.com/article/endDate", required = false) Date endDate,
并加入
@InitBinderprotected 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 )); }}
【springboot:接收date类型的参数方式】以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
推荐阅读
- Activiti(一)SpringBoot2集成Activiti6
- SpringBoot调用公共模块的自定义注解失效的解决
- 解决SpringBoot引用别的模块无法注入的问题
- springboot使用redis缓存
- springboot整合数据库连接池-->druid
- JavaScript|JavaScript — call()和apply()、Date对象、Math、包装类、字符串的方法
- SpringBoot中YAML语法及几个注意点说明
- springboot结合redis实现搜索栏热搜功能及文字过滤
- springboot中.yml文件的值无法读取的问题及解决
- SpringBoot整合MongoDB完整实例代码