json传递数据时日期格式变为LONG型报错

来源:互联网 发布:lol流浪法师咋样知乎 编辑:程序博客网 时间:2024/06/02 06:29

昨天项目上有个BUG,报了这个错误:


Response content:
{"result":"-1","resultMsg":"java.lang.IllegalArgumentException:
Cannot invoke com.hotent.core.model.BaseModel.setUpdatetime on bean class 'class com.hotent.platform.model.system.Resources'
- argument type mismatch - had objects of type \"java.lang.Long\" but expected signature \"java.util.Date\""}。

调接口的时候传给接口的参数Resources类里Updatetime时间类型date,传递的时候json传的是Long

这样接受参数的时候就报了如上类型不匹配的错误。

解决办法:

    params.put("resources", JSON.toJSONString(resources));

改成

params.put("resources", JSON.toJSONStringWithDateFormat(resources,"yyyy-MM-dd"));


接口接收参数的时候做一下处理:

String     json      = request.getParameter("resources");
            JSONObject obj       = null;
            Resources  resources = null;
            if(StringUtils.isNotEmpty(json)){
                obj  = JSONObject.fromObject(json);
                String[] dateFormats = new String[]{"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"};  
                JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats));  
                resources = (Resources) JSONObject.toBean(obj,Resources.class);
            }

这样处理完之后就不报错了。

可是我不太理解的为什么修改之前,同样的接口,我调部署在本地的接口就不报错,调测试环境的接口就会报错。