这个时候是用的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
}
/** * 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"
}
/** * 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类型的时间戳(毫秒时间戳)
解决方法
@JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss")
/** * 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(pattern=“yyyy-MM-dd”,timezone = “GMT+8”)
/** *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
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_43296313/article/details/120847896
内容来源于网络,如有侵权,请联系作者删除!