此方案是把请求参数(JSON字符串)绑定到java对象,,@RequestBody是绑定内容体到java对象的。
问题描述:
<span style="font-size: x-small;">你好,对于如下的json数据,springmvc的数据绑定该如何做? accessionDate 2012-11-21 deptIds [{"deptId":4,"isPrimary":true}] email ewer@dsfd.com fax 3423432 gender true 其实就是我前台一表单提交的数据,extjs form提交的,关键在于deptIds的映射,数组不知怎么解决,还有就是springmvc的数据绑定好像要求javabean与json中的数据一一对应,否则也会出错</span>
如请求参数是deptIds={"deptId":4,"isPrimary":true}] 是一个json数组,此时需要绑定为一个对象,该如何实现呢?
解决方案:
类似于之前写的《扩展SpringMVC以支持更精准的数据绑定》,扩展spring的HandlerMethodArgumentResolver以支持自定义的数据绑定方式。
1、请下载附件的代码,放到工程中;
2、在RequestMappingHandlerAdapter添加自定义HandlerMethodArgumentResolver Bean;
<span style="font-size: x-small;"> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <!--线程安全的访问session--> <property name="synchronizeOnSession" value="true"/> <property name="customArgumentResolvers"> <list> <bean class="cn.javass.spring.mvc.method.annotation.RequestJsonParamMethodArgumentResolver"/> <bean class="cn.javass.spring.mvc.method.annotation.FormModelMethodArgumentResolver"/> </list> </property> </bean> </span>
//customArgumentResolvers用于注入自定义的参数解析器,此处我们注入了RequestJsonParamMethodArgumentResolver。
3、使用方式
<span style="font-size: x-small;">@RequestMapping("/list") public String list(@RequestJsonParam("list") List<Integer> list) </span>
4、测试控制器
<span style="font-size: x-small;">package cn.javass.chapter6.web.controller.jsonparam; import java.util.Arrays; import java.util.List; import java.util.Set; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import cn.javass.chapter6.model.DataBinderTestModel; import cn.javass.chapter6.model.UserModel; import cn.javass.spring.mvc.bind.annotation.RequestJsonParam; import cn.javass.spring.mvc.util.MapWapper; @Controller @RequestMapping("/jsonparam") public class JsonParamController { //ok http://localhost:9080/springmvc-chapter6/jsonparam/list?list=[1,2,34] //fail http://localhost:9080/springmvc-chapter6/jsonparam/list?list=[1,2,a] @RequestMapping("/list") public String list(@RequestJsonParam("list") List<Integer> list) { System.out.println(list); return "redirect:/success"; } //ok http://localhost:9080/springmvc-chapter6/jsonparam/set?set=[1,2,34] //fail http://localhost:9080/springmvc-chapter6/jsonparam/set?set=[1,2,a] @RequestMapping("/set") public String set(@RequestJsonParam("set") Set<Integer> set) { System.out.println(set); return "redirect:/success"; } //ok http://localhost:9080/springmvc-chapter6/jsonparam/array?array=[1,2,3] //fail http://localhost:9080/springmvc-chapter6/jsonparam/array?array=[1,2,a] @RequestMapping("/array") public String list(@RequestJsonParam("array") int[] array) { System.out.println(Arrays.toString(array)); return "redirect:/success"; } //ok http://localhost:9080/springmvc-chapter6/jsonparam/map?map={"a":1, "b":2} //fail http://localhost:9080/springmvc-chapter6/jsonparam/map?map={"a":1, "b":a} @RequestMapping("/map") public String map(@RequestJsonParam(value = "map", required=false) MapWapper<String, Integer> map) { System.out.println(map); return "redirect:/success"; } //UserModel[] //ok http://localhost:9080/springmvc-chapter6/jsonparam/array2?array=[{"username":"123"},{"username":"234"}] @RequestMapping("/array2") public String array2(@RequestJsonParam(value = "array") UserModel[] array) { System.out.println(Arrays.toString(array)); return "redirect:/success"; } //List<UserModel> //ok http://localhost:9080/springmvc-chapter6/jsonparam/list2?list=[{"username":"123"},{"username":"234"}] @RequestMapping("/list2") public String list2(@RequestJsonParam(value = "list") List<UserModel> list) { System.out.println(list); return "redirect:/success"; } //Set<UserModel> //ok http://localhost:9080/springmvc-chapter6/jsonparam/set2?set=[{"username":"123"},{"username":"234"}] @RequestMapping("/set2") public String set2(@RequestJsonParam(value = "set") Set<UserModel> set) { System.out.println(set); return "redirect:/success"; } //Map<String, UserModel> //ok http://localhost:9080/springmvc-chapter6/jsonparam/map2?map={"a":{"username":"123"},"b":{"username":"234"}} //暂不支持 Map<UserModel, UserModel> @RequestMapping("/map2") public String map2(@RequestJsonParam(value = "map") MapWapper<String, UserModel> map) { System.out.println(map); return "redirect:/success"; } //ok http://localhost:9080/springmvc-chapter6/jsonparam/model1?model={"username":123,"password":234,"realname":"zhang","workInfo":{"city":"abc","job":"abc","year":"abc"}, "schoolInfo":{"schoolType":"1","schoolName":"1","specialty":"1"}} //没有realname1 //fail http://localhost:9080/springmvc-chapter6/jsonparam/model1?model={"username":123,"password":234,"realname1":123} @RequestMapping("/model1") public String model1(@RequestJsonParam(value = "model", required=true) UserModel user) { System.out.println(user); return "redirect:/success"; } //ENUM //ok http://localhost:9080/springmvc-chapter6/jsonparam/model2?model={"state":"normal"} //List<基本类型> //ok http://localhost:9080/springmvc-chapter6/jsonparam/model2?model={"hobbyList":["film", "music"]} //Map<基本类型,基本类型> //ok http://localhost:9080/springmvc-chapter6/jsonparam/model2?model={"map":{"key":"value", "a":"b"}} @RequestMapping("/model2") public String model2(@RequestJsonParam(value = "model", required=true) DataBinderTestModel model) { System.out.println(model); return "redirect:/success"; } //List<UserModel> //ok http://localhost:9080/springmvc-chapter6/jsonparam/model3?model={"userList":[{"username":"1"},{"username":"2"}]} //Map<String,UserModel> //ok http://localhost:9080/springmvc-chapter6/jsonparam/model3?model={"userMap":{"1":{"username":"1"},"2":{"username":"2"}}} //暂不支持 类似于 Map<UserModel, UserModel> 形式 @RequestMapping("/model3") public String model3(@RequestJsonParam(value = "model") DataBinderTestModel model) { System.out.println(model); return "redirect:/success"; } } </span>
支持的spring版本:
springmvc 3.0 和 3.1.x。
支持绑定的数据:
模型、集合、数组、MapWapper(Map的一个包装器,通过getInnerMap获取真实Map)
暂时不支持JSR-303数据校验.
缺点:
1、和《扩展SpringMVC以支持更精准的数据绑定》一样的缺点。
spring自定义的参数解析器会放在默认解析器之后,不能指定order,因此如果我们@FormModel("map") Map map,此map会变成Model(请参考http://jinnianshilongnian.iteye.com/blog/1698916 第六部分、Model Map ModelMap),希望未来的版本支持自定义顺序来解决这个问题;此处我们使用MapWapper解决,可以通过MapWapper.getInnerMap()拿到我们需要的Map。
2、支持的jackson版本是1.4.2,版本比较老,spring3.2里程碑版已升级到2.x。
3、暂时没有提供JSR303数据校验。