SpringMVC接受参数

SpringMVC接受参数

  • SpringMVC 能将GET、POST请求中的参数(键值对、Json)自动转换成我们需要的类型,如基本类型、数组、List、Set、Map、POJO,以及它们的组合。

    private Logger logger = LoggerFactory.getLogger(ParameterController.class);
    
    /*接受基本类型参数
    * http://localhost:8080/param/primitive.do?id=10&name=张家乐&status=true*/
    @RequestMapping("/primitive")
    @ResponseBody
    public String primitiveType(Long id, String name, Boolean status) throws UnsupportedEncodingException {
      name = new String(name.getBytes("ISO8859-1"), "UTF-8");
      logger.info("ID:{}, NAME:{}, STATUS:{}", id, name, status);
      StringBuilder str = new StringBuilder(String.valueOf(id)).append(" ")
              .append(name).append(" ").append(status);
    
      return str.toString();
    }
    
    /*接受数组参数,该方式只能在URL中传递
    * http://localhost:8080/param/array.do?name=tom&name=rose&name=jack*/
    @RequestMapping("/array")
    @ResponseBody
    public String[] arrayType(String[] name) {
      logger.info("Array:{}", Arrays.toString(name));
      return name;
    }
    
    /*接受数组参数,通过将数组放在Body中以JSON形式传输*/
    @RequestMapping(value = "/array2", method = RequestMethod.POST)
    @ResponseBody
    public String[] arrayType2(@RequestBody String[] name) {
      logger.info("Array:{}", Arrays.toString(name));
      return name;
    }
    
    /*接受简单对象参数
    * http://localhost:8080/param/simpleObject.do
    * Content-Type:application/x-www-form-urlencoded*/
    @RequestMapping(value = "/simpleObject", method = RequestMethod.POST)
    @ResponseBody
    public User simpleObject(User user) {
      logger.info("Simple Object:{}", user.toString());
      return user;
    }
    
    
    /*接受List类型的参数
    * http://localhost:8080/param/list.do
    * Content-Type:application/json
    * [1, 2, 3, 4, 5]*/
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public List<String> listType(@RequestBody List<String> list) {
      logger.info("List:{}", Arrays.toString(list.toArray()));
      return list;
    }
    
    /*接受List类型的参数
    * http://localhost:8080/param/list2.do
    * Content-Type:application/json
    * [{"id": 10, "username": "jiale", "password": "23223"},
      {"id": 120, "username": "jiale2", "password": "2322f23"},
      {"id": 103, "username": "jial3e", "password": "232fd23"}]*/
    @RequestMapping(value = "/list2", method = RequestMethod.POST)
    @ResponseBody
    public List<User> listType2(@RequestBody List<User> list) {
      logger.info("List:{}", Arrays.toString(list.toArray()));
      return list;
    }
    
    /*接受Set类型的参数
    * http://localhost:8080/param/set.do
    * Content-Type:application/json
    * [{"id": 10, "username": "jiale", "password": "23223"},
      {"id": 120, "username": "jiale2", "password": "2322f23"},
      {"id": 103, "username": "jial3e", "password": "232fd23"}]*/
    @RequestMapping(value = "/set", method = RequestMethod.POST)
    @ResponseBody
    public Set<User> setType(@RequestBody Set<User> set) {
      set.stream().forEach(e -> {
          logger.info("Id:{}, Username:{}, Password: {}\n", e.getId(), e.getUsername(), e.getPassword());
      });
      return set;
    }
    
    
    /*接受Map类型的参数
    * http://localhost:8080/param/set.do
    * Content-Type:application/json
    * {"user01": {"id": 10, "username": "jiale", "password": "23223"},
    "user02": {"id": 10, "username": "jiale", "password": "23223"}}*/
    @RequestMapping(value = "/map", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, User> setType(@RequestBody Map<String, User> map) {
    
      Set<String> keys = map.keySet();
      keys.stream().forEach(e -> {
          User user = map.get(e);
          logger.info("Id:{}, Username:{}, Password: {}\n", user.getId(), user.getUsername(), user.getPassword());
      });
      return map;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值