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"),
@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 ));
    }
}

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

(0)

相关推荐

  • 详解SpringBoot时间参数处理完整解决方案

    在JavaWeb程序的开发过程中,接口是前后端对接的主要窗口,而接口参数的接收有时候是一个令人头疼的事情,这其中最困扰程序猿的,应该是时间参数的接收. 比如:设置一个用户的过期时间,前端到底以什么格式传递参数呢?时间戳?还是2019-12-01 22:13:00这种格式?还是其他格式? 今天我就来总结一下SpringBoot Web应用接口接收时间类型参数的问题解决方案. 注:目前我对Spring源码的掌握还不是很好,所以这一篇仅仅总结一下解决方法,后面感悟多了会重写一下! 示例代码请前往:ht

  • 关于springboot 配置date字段返回时间戳的问题

    遇到一个问题,springboot升级成2.0后,从数据库查出来的日期,用Date接收,最后直接返回给前端,在谷歌浏览器中能正常显示成yyyy-MM-dd HH:mm:ss格式.但是在IE浏览器中日期显示的是"乱码",因为springboot1.x版本的默认将Date字段返回的是时间戳 ,而谷歌.IE都会自动将时间戳转换成yyyy-MM-dd HH:mm:ss:在springboot2.0后,spring会将Date字段自动给转成UTC字符串了(在没有配置的情况下),所以date需要转

  • 基于springboot处理date参数过程解析

    这篇文章主要介绍了基于springboot处理date参数过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 前言 最近在后台开发中遇到了时间参数的坑,就单独把这个问题提出来找时间整理了一下: 正文 测试方法 bean代码: public class DateModelNoAnnotation { private Integer id; private Date receiveDate; } controller代码: @RestContr

  • 关于Springboot日期时间格式化处理方式总结

    项目中使用LocalDateTime系列作为DTO中时间的数据类型,但是SpringMVC收到参数后总报错,为了配置全局时间类型转换,尝试了如下处理方式. 注:本文基于Springboot2.x测试,如果无法生效可能是spring版本较低导致的.PS:如果你的Controller中的LocalDate类型的参数啥注解(RequestParam.PathVariable等)都没加,也是会出错的,因为默认情况下,解析这种参数是使用ModelAttributeMethodProcessor进行处理,而

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

    目录 springboot:接收date类型的参数 springboot 传递Date等实体参数时候报错 springboot:接收date类型的参数 今天有个postmapping方法,地址都正确,就是死活进不去,真是奇怪了. 终于从日志中得出些端倪,见下: 只有这个属性报错,恰恰这个属性是Date型. 这句话说得更清楚: "defaultMessage":"Failed to convert property value of type 'java.lang.String

  • MyBatis中如何接收String类型的参数实现

    在MyBatis学习初期,当parameterType的值为String<==>也就是接收String类型的参数时,我会通过value来接,如图: 通过value接收String类型的值舒适又简单,然而,直到有一天,我发现屡试不爽的value不给力了->接收String类型值的时候出了问题. 到底是什么凶残吓到了我的value->就是<bind/ >标签,大家请看: 这是配套数据表 查出来的结果让我很费解,用value接收值的时候出了这个问题(好好的"GZ&q

  • springboot接收http请求,解决参数中+号变成空格的问题

    目录 springboot接收http请求,参数中+号变成空格 小插曲 解决get请求中的问题 解决post请求中的问题 SpringBoot问题笔记:http请求参数含有特殊符号[] 解决方法:修改tomcat配置 springboot接收http请求,参数中+号变成空格 小插曲 + 在执行URLEncoder.encode(String,"UTF-8")编码后会变成 %2B + 在执行URLDecoder.decode(String,"UTF-8")编码后会变成

  • ajax接收Date类型的数据时会把数据转换为时间戳

    复制代码 代码如下: $("#test").click(function(e) { $.get( "/mgr/datacleaning/test", function(data) { console.log(data + " from $.get()"); } ); var xhr = new XMLHttpRequest(); xhr.open("GET", "/mgr/datacleaning/test"

  • springboot前端传参date类型后台处理的方式

    目录 springboot前端传参date类型后台处理 先说结论 解决方法 前端如何发送date类型的参数给后端 首先阐述一下常见的几种时间类型的区别 GET传参时 Post传参时 后端接收请求代码 模拟浏览器请求 springboot前端传参date类型后台处理 先说结论 建议大家直接使用@JsonFormat,原因如下: 1.针对json格式:在配置文件中加以下配置 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.t

  • SpringMVC @RequestBody Date类型的Json转换方式

    目录 SpringMVC @RequestBody Date类型的Json转换 通过GsonBuilder设置DateFormat的格式 以零配置框架为例 以零配置形式框架下的代码实现为例讲解 @RequestBody接收json字符串,自动将日期字符串转换为java.util.Date 1.配置springMVC可以接收json字符串 2.@Controller类代码 3.实体类对象代码 4.DateJsonSerializer类代码 5.DateJsonDeserializer类代码 Spr

  • SpringBoot接收参数使用的注解实例讲解

    目录 1.基本介绍 2.接收参数相关注解应用实例 @PathVariable 使用 @RequestHeader 使用 @RequestParam 使用 @CookieValue 使用 @RequestBody 使用 3.复杂参数 1.基本介绍 2.复杂参数应用实例 4.自定义对象参数-自动封装 1.基本介绍 2.自定义对象参数-应用实例 1.基本介绍 SpringBoot 接收客户端提交数据/参数会使用到相关注解 详 解 @PathVariable . @RequestHeader . @Mo

  • 解决mybatis 数据库date 与 java中Date类型映射问题

    使用mybatis 从数据库中查询出date 类型字段,在java 类型中只看到了日期,没有看到时分秒, 从数据库中是可以看到时分秒的.后来发现是mybatis 映射数据类型的原因: 如: <result column="CREATEDATE" property="createdate" jdbcType="Date" /> 映射出来的时间格式时分秒都为0 (2017-01=12 00:00:00) <result column

  • SpringMVC接收java.util.Date类型数据的2种方式小结

    SpringMVC接收java.util.Date类型数据 在Controller中如下定义方法 public PassQueryRequest trade(@ModelAttribute PassQueryRequest tradeRequest, @RequestParam(value="startDate", required=true)Date startDate, @RequestParam(value="endDate", required=true)D

随机推荐