品牌设计策划公司,龙华网站优化,建个网站做外贸,北京logo设计公司哪家好springboot前端传参date类型后台处理方式
先说结论#xff1a;建议大家直接使用JsonFormat#xff0c;原因如下#xff1a;
1、针对json格式#xff1a;在配置文件中加以下配置 spring.jackson.date-formatyyyy-MM-dd HH:mm:ssspring.jackson.time-zoneGMT82、针对form表…springboot前端传参date类型后台处理方式
先说结论建议大家直接使用JsonFormat原因如下
1、针对json格式在配置文件中加以下配置 spring.jackson.date-formatyyyy-MM-dd HH:mm:ssspring.jackson.time-zoneGMT82、针对form表单格式加下面这句配置就可以 spring.mvc.dateFormat yyyy-MM-dd HH:mm:ss3、也可以在pojo中对特定的date类型属性加了以下配置 DateTimeFormat来控制入参JsonFormat来控制出参DateTimeFormat(patternyyyy-MM-dd HH:mm:ss)JsonFormat(timezone GMT8,pattern yyyy-MM-dd HH:mm:ss) 4、前端以字符串的形式给后台传递带有格式的 日期 和 数字 数据导致后台无法解析数据 解决方法 总结
1.如果前后端传的数据都是json格式那么后台接数据传数据都可以用JsonFormat ;
2.DateTimeFormat适合后端接收前端传来的数据不管是不是json格式都可以正确转换成Date型数据只要前端传来的格式正确且后端DateTimeFormat的pattern写正确。但是这个注解无法将Date型数据用json传到前端去
综上所述建议大家直接使用JsonFormat
Springboot的bean实体类接收Date类型 GetMapping(/test)public ResponseResult docList(QueryListParam queryListParam) {return testService.queryList(queryListParam);}
Data
public class QueryListParam implements Serializable {/*** 姓名*/private String name;/*** 起始时间接收不到会报错*/private Date startTime;/*** 结束时间接收不到会报错*/private Date endTime;
}GET请求直接传递 startTime2020-01-01 01:10:00 是接收不到了后台会报错需要在bean中Date类型的属性上添加注解DateTimeFormat(pattern “yyyy-MM-dd HH:mm:ss”) 该注解是org.springframework.format.annotation下的
Data
public class QueryListParam implements Serializable {/*** 姓名*/private String name;/*** 起始时间接收成功*/DateTimeFormat(pattern yyyy-MM-dd HH:mm:ss)private Date startTime;/*** 结束时间接收成功*/DateTimeFormat(pattern yyyy-MM-dd HH:mm:ss)private Date endTime;
}但是不用这种方法用String进行接收在用SimpleDateFormat进行转换。