昨天项目上有个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);
}
这样处理完之后就不报错了。
可是我不太理解的为什么修改之前,同样的接口,我调部署在本地的接口就不报错,调测试环境的接口就会报错。
在项目中遇到一个BUG,当通过JSON传递包含日期的Resources对象时,日期被转换为Long型,导致接口接收到参数时出现类型不匹配的错误。解决方法是在发送参数时使用`JSON.toJSONStringWithDateFormat`将日期格式化,并在接口接收端利用`JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats))`进行日期转换。奇怪的是,该问题仅在调用测试环境接口时出现,而在本地环境中并未报错。
2997

被折叠的 条评论
为什么被折叠?



