springboot前端传参date类型后台处理的方式
作者:香草天空Sky 时间:2023-04-12 16:47:35
springboot前端传参date类型后台处理
先说结论
建议大家直接使用@JsonFormat,原因如下:
1、针对json格式:在配置文件中加以下配置
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
2、针对form表单格式,加下面这句配置就可以
spring.mvc.dateFormat = yyyy-MM-dd HH:mm:ss
3、也可以在pojo中对特定的date类型属性加了以下配置
@DateTimeFormat来控制入参,@JsonFormat来控制出参
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
4、前端以字符串的形式给后台传递 带有格式的 日期 和 数字 数据,导致后台无法解析数据:
解决方法
总结:
1.如果前后端传的数据都是json格式,那么后台接数据,传数据都可以用@JsonFormat ;
2.@DateTimeFormat适合后端接收前端传来的数据,不管是不是json格式都可以正确转换成Date型数据,只要前端传来的格式正确且后端@DateTimeFormat的pattern写正确。但是,这个注解无法将Date型数据用json传到前端去
综上所述,建议大家直接使用@JsonFormat
前端如何发送date类型的参数给后端
首先阐述一下常见的几种时间类型的区别
java.util.Date
日期格式为:年月日时分秒java.sql.Date
日期格式为:年月日java.sql.Time
日期格式为:时分秒java.sql.Timestamp
日期格式为:年月日时分秒纳秒(毫微秒)
前端传时间类型的参数给后端,一般有两种传参手段,GET传参和POST传参。
GET传参时
前段传过来的是一个string的字符串,后端用string类型接接收后需要做相关处理。处理代码如下:
String str="2021-5-21"; //假设str为前段传过来的时间类型参数
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date parse = simpleDateFormat.parse(str);
String format = simpleDateFormat.format(parse);
System.out.println(parse);
System.out.println(format);
Post传参时
前端传过来的是一个对象,时间参数只是对象当中的一个属性,该对象案例如下。
public class Student {
public String getName() {
return name;
}
public Date getDate() {
return date;
}
private String name;
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") //在时间属性上面加上该注解
private Date date;
}
后端接收请求代码
@RestController
@RequestMapping("/test")
public class TestTime {
@GetMapping("/time1")
public void service1(String time){
System.out.println(time);
}
@PostMapping("/time2")
public void service2(@RequestBody Student student){
System.out.println(student);
}
}
模拟浏览器请求
### GET 请求
GET {{baseUrl}}//test/time2?time=2021-5-8
### POST 请求
POST {{baseUrl}}//test/time2
Content-Type: application/json
{
"name": "小顾",
"date": "2020-05-08 17:08:10"
}
注:baseurl是我的ip地址,模拟请求的工具很多,上述只是一种工具而已,读者可用postman等工具模拟请求。
总结:get请求是字符串,需要做处理
post请求传的是对象,通过@requestbody,和在字段上添加jsonformat,会自动解析为date类型,不需要在做额外处理。
来源:https://blog.csdn.net/qsx741olm/article/details/100129755
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java 利用反射获取内部类静态成员变量的值操作
![](https://img.aspxhome.com/file/2023/6/72336_0s.jpg)
OpenCV图像处理之常见的图像灰度变换
![](https://img.aspxhome.com/file/2023/1/135081_0s.jpg)
C#byte数组传入C操作方法
![](https://img.aspxhome.com/file/2023/9/82129_0s.png)
C# 使用CancellationTokenSource取消多线程
![](https://img.aspxhome.com/file/2023/4/108674_0s.png)
JavaSE中compare、compareTo的区别
![](https://img.aspxhome.com/file/2023/4/77134_0s.png)
spring mvc实现文件上传并携带其他参数的示例
Java中Builder模式的实现详解
Android开发两个activity之间传值示例详解
Java 详解循环屏障CyclicBarrier如何实现多线程分段等待执行完成
java参数传递之值传递和引用传递
Java实现简单的扫雷小程序
![](https://img.aspxhome.com/file/2023/6/63806_0s.jpg)
Android 媒体开发之MediaPlayer状态机接口方法实例解析
![](https://img.aspxhome.com/file/2023/3/139413_0s.gif)
Android提高之ListView实现自适应表格的方法
![](https://img.aspxhome.com/file/2023/0/130460_0s.gif)
Android基础之startActivityForResult()的用法详解
SpringBoot绿叶显示yml和端口问题及解决方法
![](https://img.aspxhome.com/file/2023/5/62775_0s.png)
SpringMvc web.xml配置实现原理过程解析
java抓取网页或文件中的邮箱号码
![](https://img.aspxhome.com/file/2023/9/62119_0s.jpg)
java实现点击按钮弹出新窗体功能
![](https://img.aspxhome.com/file/2023/4/119154_0s.jpg)
基于@PathVariable注解的用法说明
![](https://img.aspxhome.com/file/2023/4/76514_0s.jpg)
基于Java中字符串indexof() 的使用方法
![](https://img.aspxhome.com/file/2023/7/65587_0s.jpg)