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; }