请求响应-实体参数的接受

实体参数的接受

  • 简单实体对象:请求参数名与形参属性对象名相同,定义pojo接受即可,将数据封装到实体类中
  • 实体类代码如下:
    • 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 +
                      '}';
          }
      
      }
      
  • 控制类代码如下:
    •  
      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";
          }
      }
      
  •  postman中发送请求、idea接受并处理请求结果如下:
    • 请求响应-实体参数的接受_第1张图片
    • 请求响应-实体参数的接受_第2张图片
  • 复杂实体参数接受 (一个实体类中包含另一个实体类作为对象,即对象的组合

    • 参数的传递通过对象.属性=属性值的形式

      • 请求响应-实体参数的接受_第3张图片

  • 实体类的代码如下:

    • 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 + '\'' +
                      '}';
          }
      }
      
       
  • 控制类代码如下:
    • 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";
          }
      }
      
    • postman中发送请求、idea接受并处理请求结果如下:

      • 请求响应-实体参数的接受_第4张图片
      • 请求响应-实体参数的接受_第5张图片

你可能感兴趣的:(Java,Web学习跟踪笔记,spring,java)