最近在调试接口时发现:接口传递过来的jSON数据格式中时间格式是字符串的形式:
{"name":"满300送50", "merchantId":"1", "needMoney":"100", "money":"50", "useMoney":"300", "beginDatetime":"2017-09-27 10:00:00", "endDatetime":"2017-09-27 10:00:00" }
而java绑定的实体文件是Date格式的:
private Date beginDatetime; @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") public Date getBeginDatetime() { return beginDatetime; } public void setBeginDatetime(Date beginDatetime) { this.beginDatetime = beginDatetime; }
调用接口的时候就会报HTTP Status 400 – Bad Request的错误,表示
The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).
在getter方法上添加@JsonFormat注解
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") public Date getBeginDatetime() { return beginDatetime; }
本文出自 “一个程序媛的自我修养” 博客,请务必保留此出处http://qianqiansun.blog.51cto.com/13271301/1968950
原文地址:http://qianqiansun.blog.51cto.com/13271301/1968950