springboot:接收date类型的参数方式

网友投稿 805 2022-12-01

springboot:接收date类型的参数方式

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",

"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")fhHUeTi,

@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(fhHUeTi"startDate").as(Date.class), startDate ));

predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("endDate").as(Date.class), startDate ));

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:[笔记]攻防工具分享之 CobaltStrike框架 《二》生成后门
下一篇:【周赛复盘】力扣第 305 场单周赛
相关文章

 发表评论

暂时没有评论,来抢沙发吧~