SpringBoot 方式
简单参数请求
参数名称与形参变量名相同,即可接收参数
1 2 3 4 5 6 7 8
| @RestController public class SimpleParamController { @RequestMapping("/simpleParam") public String simpleParam(String name, Integer age) { System.out.println(name + ":" + age); return "OK"; } }
|
实体对象参数
1 2 3 4 5
| @RequestMapping("/simplePojo") public String simpleParam(User user) { System.out.println(user); return "OK"; }
|
1 2 3 4 5 6
| public class User { private String name; private Integer age; }
|
复杂实体对象参数
参数对象有 对象嵌套
1 2 3 4 5 6 7 8 9 10
| public class User { private String name; private Integer age; private Address address; }
public class Address { private String province; private String city; }
|
请求的参数写做:
GET: localhost:8080/...&address.province=beijing&address.city=beijing
数组集合请求
1 2 3 4 5
| @RequestMapping("/arrayParam") public String arrayParam(String[] hobby) { System.out.println(Arrays.toString(hobby)); return "OK"; }
|
请求写作:
localhost:8080/arrayParam?hobby=打球&hobby=玩游戏&hobby=唱歌
默认封装到数组中,可修改成集合:
1 2 3 4 5
| @RequestMapping("/listParam") public String listParam(String[] hobby) { System.out.println(Arrays.toString(hobby)); return "OK"; }
|