fastjson转换包含date类型属性的对象时报错com.alibaba.fastjson.JSONException: For input string: "13:02:19"

问题: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;

 

posted @ 2016-05-31 10:16  梦尽落花  阅读(50309)  评论(1编辑  收藏  举报