您的位置:首页 > Web前端 > JavaScript

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

2017-11-02 11:14 507 查看
昨天项目上有个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);

            }

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

可是我不太理解的为什么修改之前,同样的接口,我调部署在本地的接口就不报错,调测试环境的接口就会报错。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  json