java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java前端发送date类型的参数给后端

Java前端实现发送date类型的参数给后端

作者:yololee_

这篇文章主要介绍了Java前端实现发送date类型的参数给后端方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

@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类型的时间戳(毫秒时间戳)

解决方法:

@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”)
/**
     *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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文