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

x33g5p2x  于2021-10-20 转载在 Java  
字(4.1k)|赞(0)|评价(0)|浏览(365)

@DateTimeFormat

第一次:Get方式传参-成功

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

/** * http://localhost:8080/intoParam?date=2019-01-18 11:11:11 */
    @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方式传参-失败

/** * http://localhost:8080/intoParam * 请求体 * { * "date":"2019-01-18 11:11:11" * } */
    @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传参-成功

/** * http://localhost:8080/intoParam * 請求體是: * { * "date":"2019-01-18" * } */
    @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传参-成功

/** * http://localhost:8080/intoParam * 請求體是: * { * "date":"2019-01-18 11:11:11" * } */
    @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方法上,两种方式没有区别
/** *http://localhost:8080/outParam */
    @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

相关文章