栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java前端如何发送date类型的参数给后端

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java前端如何发送date类型的参数给后端

Java前端如何发送date类型的参数给后端 @DateTimeFormat 第一次:Get方式传参-成功

这个时候是用的get请求方式,get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应

    @RequestMapping(value = "/intoParam",method = RequestMethod.GET)
    @ResponseBody
    public void intoParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date date){
        System.out.println(date);//Fri Jan 18 08:00:00 CST 2019
    }
第二次:Post方式传参-失败
    @RequestMapping(value = "/intoParam",method = RequestMethod.POST)
    @ResponseBody
    public void intoParam2(@RequestBody DateVo dateVo){
        System.out.println(dateVo.getDate());//Fri Jan 18 08:00:00 CST 2019
    }

@Data
@AllArgsConstructor
@NoArgsConstructor
public class DateVo {

    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    private Date date;
}

错误信息

{
	"timestamp": "2021-10-19T07:05:22.407+0000",
	"status": 400,
	"error": "Bad Request",
	"message": "JSON parse error: Cannot deserialize value of type `java.util.Date` from String "2019-01-18 11:11:11": not a valid representation (error: Failed to parse Date value '2019-01-18 11:11:11': Cannot parse date "2019-01-18 11:11:11": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2019-01-18 11:11:11": not a valid representation (error: Failed to parse Date value '2019-01-18 11:11:11': Cannot parse date "2019-01-18 11:11:11": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? null))n at [Source: (PushbackInputStream); line: 2, column: 12] (through reference chain: com.mye.hl20springbootdataparam.vo.DateVo["date"])",
	"path": "/intoParam"
}
第三次:post传参-成功
    @RequestMapping(value = "/intoParam",method = RequestMethod.POST)
    @ResponseBody
    public void intoParam2(@RequestBody DateVo dateVo){
        System.out.println(dateVo.getDate());//Fri Jan 18 08:00:00 CST 2019
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(dateVo.getDate());
        System.out.println(format);//2019-01-18
    }

@Data
@AllArgsConstructor
@NoArgsConstructor
public class DateVo {

    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date date;
}
原因

springboot默认采用jackson,而jackson只能识别以下几种日期格式

"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
 
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
 
"yyyy-MM-dd";
 
"EEE, dd MMM yyyy HH:mm:ss zzz";
 
long类型的时间戳(毫秒时间戳)

解决方法

  • 采用long时间戳,如:1537191968000
  • 在传参的对象上加上@JsonFormat注解并且指定时区
@JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss")
第四次:post传参-成功
    @RequestMapping(value = "/intoParam",method = RequestMethod.POST)
    @ResponseBody
    public void intoParam2(@RequestBody DateVo dateVo){
        System.out.println(dateVo.getDate());//Fri Jan 18 11:11:11 CST 2019
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(dateVo.getDate());
        System.out.println(format);//2019-01-18 11:11:11
    }

@Data
@AllArgsConstructor
@NoArgsConstructor
public class DateVo {

    @DateTimeFormat(pattern="yyyy-MM-dd")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date date;
}
@jsonFormat

@JsonFormat(pattern=“yyyy-MM-dd”,timezone = “GMT+8”)

  • pattern:是你需要转换的时间日期的格式
  • timezone:是时间设置为东八区,避免时间在转换中有误差
  • @JsonFormat注解可以在属性的上方,同样可以在属性对应的get方法上,两种方式没有区别
    @RequestMapping(value = "/outParam",method = RequestMethod.GET)
    @ResponseBody
    public DateVo intoParam2(){
       return new DateVo(new Date());
    }

@Data
@AllArgsConstructor
@NoArgsConstructor
public class DateVo {

//    @DateTimeFormat(pattern="yyyy-MM-dd")
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date date;
}

展示结果

{
	"date": "2021-10-19 15:44:51"
}
总结

前端Content-Type 为application/json的请求时,我们使用@JsonFormat来进行转化,如果为表单,则应该使用@DateTimeFormat

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/337490.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号