推荐先阅读
:Spring全家桶
普通参数
GET http://localhost/commonParamDifferentName?name=张三&age=18
|
@RequestMapping("/commonParamDifferentName") @ResponseBody public String commonParamDifferentName(@RequestPaam("name") String userName , int age){ System.out.println("普通参数传递 userName ==> "+userName); System.out.println("普通参数传递 age ==> "+age); return "{'module':'common param different name'}"; }
|
@RequestPaam("name")
解决参数不一致问题
注意:写上@RequestParam注解框架就不需要自己去解析注入,能提升框架处理性能
POJO类型参数
public class User { private String name; private int age; }
|
GET http://localhost/commonParamDifferentName?name=zhangxin&age=18
|
@RequestMapping("/pojoParam") @ResponseBody public String pojoParam(User user){ System.out.println("pojo参数传递 user ==> "+user); return "{'module':'pojo param'}"; }
|
嵌套POJO类型参数
public class Address { private String province; private String city; } public class User { private String name; private int age; private Address address; }
|
GET http://localhost/commonParamDifferentName?name=zhangxin&age=18&address.city=hangzhou&address.province=zhejiang
|
@RequestMapping("/pojoParam") @ResponseBody public String pojoParam(User user){ System.out.println("pojo参数传递 user ==> "+user); return "{'module':'pojo param'}"; }
|
数组类型参数
GET http://localhost/arrayParam?likes=game&likes=music&likes=travel
|
@RequestMapping("/arrayParam") @ResponseBody public String arrayParam(String[] likes){ System.out.println("数组参数传递 likes ==> "+ Arrays.toString(likes)); return "{'module':'array param'}"; }
|
集合类型参数
GET http://localhost/arrayParam?likes=game&likes=music&likes=travel
|
@RequestMapping("/listParam") @ResponseBody public String listParam(@RequestParam List<String> likes){ System.out.println("集合参数传递 likes ==> "+ likes); return "{'module':'list param'}"; }
|
知识点1:@RequestParam
名称 |
@RequestParam |
类型 |
形参注解 |
位置 |
SpringMVC控制器方法形参定义前面 |
作用 |
绑定请求参数与处理器方法形参间的关系 |
相关参数 |
required:是否为必传参数 defaultValue:参数默认值 |