扩展SpringMVC以支持绑定JSON格式的请求参数

http://jinnianshilongnian.iteye.com/blog/1719952


  1. <span style="font-size: x-small;">package cn.javass.chapter6.web.controller.jsonparam;  
  2.   
  3. import java.util.Arrays;  
  4. import java.util.List;  
  5. import java.util.Set;  
  6.   
  7. import org.springframework.stereotype.Controller;  
  8. import org.springframework.web.bind.annotation.RequestMapping;  
  9.   
  10. import cn.javass.chapter6.model.DataBinderTestModel;  
  11. import cn.javass.chapter6.model.UserModel;  
  12. import cn.javass.spring.mvc.bind.annotation.RequestJsonParam;  
  13. import cn.javass.spring.mvc.util.MapWapper;  
  14.   
  15.   
  16. @Controller  
  17. @RequestMapping("/jsonparam")    
  18. public class JsonParamController {  
  19.   
  20.     //ok   http://localhost:9080/springmvc-chapter6/jsonparam/list?list=[1,2,34]  
  21.     //fail http://localhost:9080/springmvc-chapter6/jsonparam/list?list=[1,2,a]  
  22.     @RequestMapping("/list")    
  23.     public String list(@RequestJsonParam("list") List<Integer> list) {  
  24.         System.out.println(list);  
  25.         return "redirect:/success";          
  26.     }  
  27.       
  28.     //ok   http://localhost:9080/springmvc-chapter6/jsonparam/set?set=[1,2,34]  
  29.     //fail http://localhost:9080/springmvc-chapter6/jsonparam/set?set=[1,2,a]  
  30.     @RequestMapping("/set")    
  31.     public String set(@RequestJsonParam("set") Set<Integer> set) {  
  32.         System.out.println(set);  
  33.         return "redirect:/success";          
  34.     }  
  35.       
  36.       
  37.     //ok   http://localhost:9080/springmvc-chapter6/jsonparam/array?array=[1,2,3]  
  38.     //fail http://localhost:9080/springmvc-chapter6/jsonparam/array?array=[1,2,a]  
  39.     @RequestMapping("/array")    
  40.     public String list(@RequestJsonParam("array"int[] array) {  
  41.         System.out.println(Arrays.toString(array));  
  42.         return "redirect:/success";          
  43.     }  
  44.       
  45.       
  46.     //ok   http://localhost:9080/springmvc-chapter6/jsonparam/map?map={"a":1, "b":2}  
  47.     //fail http://localhost:9080/springmvc-chapter6/jsonparam/map?map={"a":1, "b":a}  
  48.     @RequestMapping("/map")    
  49.     public String map(@RequestJsonParam(value = "map", required=false) MapWapper<String, Integer> map) {  
  50.         System.out.println(map);  
  51.         return "redirect:/success";          
  52.     }  
  53.       
  54.       
  55.     //UserModel[]  
  56.     //ok   http://localhost:9080/springmvc-chapter6/jsonparam/array2?array=[{"username":"123"},{"username":"234"}]  
  57.     @RequestMapping("/array2")    
  58.     public String array2(@RequestJsonParam(value = "array") UserModel[] array) {  
  59.         System.out.println(Arrays.toString(array));  
  60.         return "redirect:/success";          
  61.     }  
  62.   
  63.     //List<UserModel>  
  64.     //ok   http://localhost:9080/springmvc-chapter6/jsonparam/list2?list=[{"username":"123"},{"username":"234"}]  
  65.     @RequestMapping("/list2")    
  66.     public String list2(@RequestJsonParam(value = "list") List<UserModel> list) {  
  67.         System.out.println(list);  
  68.         return "redirect:/success";          
  69.     }  
  70.   
  71.     //Set<UserModel>  
  72.     //ok   http://localhost:9080/springmvc-chapter6/jsonparam/set2?set=[{"username":"123"},{"username":"234"}]  
  73.     @RequestMapping("/set2")    
  74.     public String set2(@RequestJsonParam(value = "set") Set<UserModel> set) {  
  75.         System.out.println(set);  
  76.         return "redirect:/success";          
  77.     }  
  78.       
  79.     //Map<String, UserModel>  
  80.     //ok   http://localhost:9080/springmvc-chapter6/jsonparam/map2?map={"a":{"username":"123"},"b":{"username":"234"}}  
  81.     //暂不支持 Map<UserModel, UserModel>  
  82.     @RequestMapping("/map2")    
  83.     public String map2(@RequestJsonParam(value = "map") MapWapper<String, UserModel> map) {  
  84.         System.out.println(map);  
  85.         return "redirect:/success";          
  86.     }  
  87.       
  88.       
  89.     //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"}}  
  90.     //没有realname1  
  91.     //fail http://localhost:9080/springmvc-chapter6/jsonparam/model1?model={"username":123,"password":234,"realname1":123}  
  92.     @RequestMapping("/model1")    
  93.     public String model1(@RequestJsonParam(value = "model", required=true) UserModel user) {  
  94.         System.out.println(user);  
  95.         return "redirect:/success";          
  96.     }  
  97.       
  98.     //ENUM  
  99.     //ok   http://localhost:9080/springmvc-chapter6/jsonparam/model2?model={"state":"normal"}  
  100.     //List<基本类型>  
  101.     //ok   http://localhost:9080/springmvc-chapter6/jsonparam/model2?model={"hobbyList":["film", "music"]}  
  102.     //Map<基本类型,基本类型>  
  103.     //ok   http://localhost:9080/springmvc-chapter6/jsonparam/model2?model={"map":{"key":"value", "a":"b"}}      
  104.     @RequestMapping("/model2")    
  105.     public String model2(@RequestJsonParam(value = "model", required=true) DataBinderTestModel model) {  
  106.         System.out.println(model);  
  107.         return "redirect:/success";          
  108.     }  
  109.       
  110.     //List<UserModel>  
  111.     //ok   http://localhost:9080/springmvc-chapter6/jsonparam/model3?model={"userList":[{"username":"1"},{"username":"2"}]}  
  112.     //Map<String,UserModel>  
  113.     //ok   http://localhost:9080/springmvc-chapter6/jsonparam/model3?model={"userMap":{"1":{"username":"1"},"2":{"username":"2"}}}  
  114.   
  115.     //暂不支持 类似于 Map<UserModel, UserModel> 形式  
  116.     @RequestMapping("/model3")    
  117.     public String model3(@RequestJsonParam(value = "model") DataBinderTestModel model) {  
  118.         System.out.println(model);  
  119.         return "redirect:/success";          
  120.     }  
  121.       
  122. }  
  123. </span>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值