Smail_的gravatar头像
Smail_ 2016-10-21 12:05:47
Spring MVC接收参数的那些坑总结

Spring大法好!

然而,有时候坑也不少......

下面是我躺过的几个坑(现在已碰到过的):

第一坑:时间参数绑定

客户端时间:yyyy-mm-dd hh:mm:ss

服务端时间:Date

难道客户端传给我的不是时间?!

扯淡,直接报时间参数绑定错误,下图是错误:

Spring MVC接收参数的那些坑总结

解决方案:

/**
	 * 方法名:initBinder
	 * 功能描述: 数据绑定重载
	 *
	 * @author yangtao
	 * @since 2016年9月5日 下午5:37:53
	 *
	 * @param binder
	 */
    @InitBinder    
    protected void initBinder(WebDataBinder binder) {
    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    	format.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(format, true));  
    }

第二坑:接收List参数

发送方式:restTemplate.postForObject(url + "/getDistinctOwnerList", new HttpEntity<List<String>>(list), List.class)

接收方式:

@RequestMapping("/getDistinctOwnerList")
public List<Owner> getDistinctOwnerList(List<String> areasIDList)

exception:java.util.List 是接口不能实例化

改成:ArrayList,继续错误。

解决:

areasID = list拼接后的字符串

restTemplate.getForObject(url + "/getDistinctOwnerList?areasID={areasID}",Owner[].class, areasID)

getDistinctOwnerList(@RequestParam(value="areasID", required=false)List<String> areasIDList);

第三坑:接收的参数比实际传的参数多

这个坑比较好解决:没有的参数加上 @RequestParam(value="参数名", required=false)即可


打赏
最近浏览
wkc  LV21 2020年6月28日
梦醒繁华丶  LV19 2018年4月27日
wgc_jy  LV21 2018年2月2日
hanl  LV12 2018年1月25日
yjx4510013  LV9 2017年12月20日
203778513  LV9 2017年9月13日
04300115116 2017年6月1日
暂无贡献等级
jiky  LV2 2017年5月9日
shanks2015  LV2 2017年4月14日
lvcai88  LV4 2017年4月13日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友