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

 更新时间:2021年10月14日 11:51:47   作者:huiy_小溪  
这篇文章主要介绍了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 ));
    }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Spring @Conditional注解示例详细讲解

    Spring @Conditional注解示例详细讲解

    @Conditional是Spring4新提供的注解,它的作用是按照一定的条件进行判断,满足条件给容器注册bean,这篇文章主要介绍了Spring @Conditional注解示例详细讲解,需要的朋友可以参考下
    2022-11-11
  • 自定义Jackson的ObjectMapper如何实现@ResponseBody的自定义渲染

    自定义Jackson的ObjectMapper如何实现@ResponseBody的自定义渲染

    这篇文章主要介绍了自定义Jackson的ObjectMapper如何实现@ResponseBody的自定义渲染,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07
  • SpringMVC实现文件下载功能

    SpringMVC实现文件下载功能

    这篇文章主要为大家详细介绍了SpringMVC实现文件下载功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • Springboot拦截filter中异常的处理

    Springboot拦截filter中异常的处理

    SpringBoot提供了全局异常处理机制可以拦截所有异常,包括Filter中的异常,本文主要介绍了Springboot拦截filter中异常的处理,具有一定的参考价值,感兴趣的可以了解一下
    2023-11-11
  • Java实现矩形碰撞检测

    Java实现矩形碰撞检测

    这篇文章主要为大家详细介绍了Java实现矩形碰撞检测,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-06-06
  • 关于Spring的@Autowired依赖注入常见错误的总结

    关于Spring的@Autowired依赖注入常见错误的总结

    有时我们会使用@Autowired自动注入,同时也存在注入到集合、数组等复杂类型的场景。这都是方便写 bug 的场景,本篇文章带你了解Spring @Autowired依赖注入的坑
    2021-09-09
  • FastJson踩坑:@JsonField在反序列化时失效的解决

    FastJson踩坑:@JsonField在反序列化时失效的解决

    这篇文章主要介绍了FastJson踩坑:@JsonField在反序列化时失效的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • Springboot分模块项目搭建的实现

    Springboot分模块项目搭建的实现

    在软件开发中,利用Spring Boot进行分模块项目搭建能够提高代码的模块化和复用性,本文主要介绍了Springboot分模块项目搭建的实现,感兴趣的可以了解一下
    2024-10-10
  • 解决idea默认带的equals和hashcode引起的bug

    解决idea默认带的equals和hashcode引起的bug

    这篇文章主要介绍了解决idea默认带的equals和hashcode引起的bug,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • 解析springcloud中的Hystrix

    解析springcloud中的Hystrix

    Hystrix是一个用于处理分布式系统的延迟和容错的开源库,在分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等。这篇文章主要介绍了springcloud中的Hystrix,需要的朋友可以参考下
    2020-10-10

最新评论