Acceptance of Entity Parameters
- Simple entity object: the request parameter name is the same as the formal parameter attribute object name, just define pojo to accept it, and encapsulate the data into the entity class
- The entity class code is as follows:
-
package com.example.POJO; public class User { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } public Integer getAge() { return age; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
-
- The control class code is as follows:
-
package com.example.Controller; import com.example.POJO.User; import jakarta.servlet.http.HttpServletRequest; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; // TODO 实体参数接受 @RequestMapping("/simplePojo") public String simplePojo(User user) { System.out.println(user); return "OK"; } }
-
- The request is sent in postman, and the idea accepts and processes the request. The results are as follows:
-
Complex entity parameters accept (an entity class contains another entity class as an object, that is, a combination of objects )
-
Parameters are passed in the form of object. attribute = attribute value
-
-
The code of the entity class is as follows:
-
User
package com.example.POJO; public class User { // TODO 对象的组合(一个实体类成员变量是另一个实体类的对象) private Address address; private String name; public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } public Integer getAge() { return age; } @Override public String toString() { return "User{" + "address=" + address + ", name='" + name + '\'' + ", age=" + age + '}'; } }
- Address
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; } @Override public String toString() { return "Address{" + "province='" + province + '\'' + ", city='" + city + '\'' + '}'; } }
-
- The control class code is as follows:
-
package com.example.Controller; import com.example.POJO.User; import jakarta.servlet.http.HttpServletRequest; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController // TODO 复杂参数接受 @RequestMapping("/complexParam") public String complexParam(User user) { System.out.println(user); return "ok"; } }
- The request is sent in postman, and the idea accepts and processes the request. The results are as follows:
-