简单参数
-
原始方式
在原始的web程序中,获取请求参数,需要通过HttpServletRequest对象手动获取。
@RequestMapping("/simpleParam") public String simpleParam(HttpServletRequest request) { String name = request.getParameter("name"); String age = request.getParameter("age"); System.out.println("Name: " + name + " Age: " + age); return "Name: " + name + " Age: " + age; }
-
SpringBoot方式
- 请求参数名与方法形参变量名相同
- 会自动进行类型转换
@RequestMapping("/simpleParam") public String simpleParam(String name, Integer age) { System.out.println("Name: " + name + " Age: " + age); return "Name: " + name + " Age: " + age; }
如果方法形参名称与请求参数名称不匹配,可以使用
@RequestParam
完成映射。@RequestMapping("/simpleParam") public String simpleParam(@RequestParam(name="Name") String name, Integer age) { System.out.println("Name: " + name + " Age: " + age); return "Name: " + name + " Age: " + age; }
注意:
@RequestParam
中的required属性默认为true,代表该请求参数必须传递,如果不传递将报错。如果该参数是可以选的,可以将required属性设置为false。@RequestMapping("/simpleParam") public String simpleParam(@RequestParam(name="Name", required = false) String name, Integer age) { System.out.println("Name: " + name + " Age: " + age); return "Name: " + name + " Age: " + age; }
实体参数
-
简单实体参数:请求参数名与形参对象属性名相同,定义POJO接收即可。
在
pojo
目录下新建User.java
文件,并设置:package com.example.pojo; public class User { private String name; private Integer age; public User() { } public User(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
在
controller
层内添加:@RequestMapping("/simplePojo") public String simplePojo(User user) { System.out.println("Name: " + user.getName() + " Age: " + user.getAge()); return "simplePoj: Name: " + user.getName() + " Age: " + user.getAge(); }
-
复杂实体参数
新建
Address.java
文件package com.example.pojo; public class Address { private String province; private String city; public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String toString() { return "Address{" + "province='" + province + '\'' + ", city='" + city + '\'' + '}'; } }
修改
User.java
:package com.example.pojo; public class User { private String name; private Integer age; private Address address; public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public User() { } public User(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + ", address=" + address + '}'; } }
在
controller
层添加新GET:@RequestMapping("/complexPojo") public String complexPojo(User user) { System.out.println(user); return "complexPojo: " + user; }