問題:time類型數據插入不進mysql數據庫;調試的時候報如下錯誤:
Caused by: java.lang.NumberFormatException: For input string: "13:02:19"
Resolving exception from handler [public slc.utils.ResultJson slc.controller.SamplingpointinfoController.saveSamplingpointinfo(java.lang.String) throws java.lang.Exception]: com.alibaba.fastjson.JSONException: For input string: "13:02:19"
斷點調試, System.out.println(t);傳入的參數能打印出來,
{
"samplingpointId":"1",
"samplingDate":"2016-04-13",
"samplingTime":"13:02:19",
"samplingpointType":"0",
"samplingpointVariety":"測試1",
"samplingpeopleId":"1",
"samplingArea":"測試1"
}
進了 SamplingpointinfoTable samplingpointinfoTable=JSON.parseObject(t, SamplingpointinfoTable.class);這句代碼拋異常;
代碼部分:
public @ResponseBody ResultJson saveSamplingpointinfo(@RequestParam(value="json",required=true) String t) throws Exception{
System.out.println(t);
SamplingpointinfoTable samplingpointinfoTable=JSON.parseObject(t, SamplingpointinfoTable.class);
int DBResponse=samplingpointinfoServiceI.insertSelective(samplingpointinfoTable);
於是基本斷定是傳入的參數沒有轉換成json對象成功。網上說要fastjson對日期型處理時要在pojo中相應屬性下面加注解
@JSONField(format="HH:mm:ss")
private Date samplingTime;
改完后繼續調試,可是為什么fastjson轉換還是不成功呢,
mysql中date(2015-05-31)和time(20:21:56)在java中對應的都是date類型,使用mybatis逆向工程生成的也都是date類型,這些也都沒問題啊
是不是這個版本有bug啊,我使用的
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.26</version>
</dependency>
換成最新版吧,
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.12</version>
</dependency>
還真是版本問題,換成這個版本以后就好了;然后為了試試到底用不用加注解,我又把下面pojo里面的屬性上的注解去掉了
@JSONField(format="HH:mm:ss")
private Date samplingTime;
去掉就報錯!所以總結:1,fastjson 1.1.26版本存在date類型轉換的bug,換成新版本就好;
2,pojo里面的date屬性上要加注解,比如:@JSONField(format="HH:mm:ss")
private Date samplingTime;