postman测试@pathvariable,@requestparam,@requestbody发送情况
1、三种方式简单说明
1.1、@Pathvariable
通过占位符的方式获取入参,前端示例:url:http://localhost:8080/system/student/${stuSno}
 也即是从路径里面去获取变量
 后端:
    /**
     * @param stuSno 学号
     * @return 学生信息
     * @description 根据主键获取学生信息
     */
    @GetMapping("/selectByPrimaryKey/{stuSno}")
    public Student selectByPrimaryKey(@PathVariable String stuSno) {
        return studentService.selectByPrimaryKey(stuSno);
    }
这种情况是方法参数名称和需要绑定的url中变量名称一致时
 若是若方法参数名称和需要绑定的url中变量名称不一致时
 后端:
/**
 * @param stuSno 学号
 * @return 学生信息
 * @description 根据主键获取学生信息
 */
@GetMapping("/selectByPrimaryKey/{stuSno}")
public Student selectByPrimaryKey(@PathVariable("stuSno") String sno) {
    return studentService.selectByPrimaryKey(stuSno);
}
注意:前端传参的URL于后端@RequestMapping的URL必须相同且参数位置一一对应,否则前端会找不到后端地址
1.2、@RequestParam
- 作用
将请求参数绑定在控制层(controller)方法参数【springmvc注解】 - 语法
 
@RequestParam(value="参数名",required="true/false",default="")

value:表示前端传过来的值名称,如果你不设置,那就默认使用服务端使用的参数名称(stuSno)
 不设置:
 前端:
http://localhost:8081/student/selectByPrimaryKey1?stuSno=0001
后端:
public Student selectByPrimaryKey1(@RequestParam String stuSno) {
设置
 前端:
http://localhost:8081/student/selectByPrimaryKey1?sno=0001
后端:
 此时@requestParam中value=“sno” value可以省略 直接输入“sno”,类似于@RequestMapping
    public Student selectByPrimaryKey1(@RequestParam("sno") String stuSno) {
这时候前端传sno并非stuSno,需要在@requestParam中value设置sno
required:是否包含该参数,默认为true,表示该请求路径中必须包含该参数,如果不包含就报错
2021-08-15 02:26:30.495  WARN 4736 --- [nio-8081-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required request parameter 'sno' for method parameter type String is not present]
defaultValue:默认参数值,如果设置了该值,required=true将失效,自动变为false,如果没有传该参数,使用默认值;比如说此时 后端直接写成 defaultValue=“0001”

- 示例说明
后端controller 
    /**
     * @param stuSno 学号
     * @return 学生信息
     * @description 根据主键获取学生信息
     */
    @GetMapping("/selectByPrimaryKey")
    public Student selectByPrimaryKey1(@RequestParam(value = "sno",defaultValue = "0001",required = false) String stuSno) {
        return studentService.selectByPrimaryKey(stuSno);
    }
前端暂时使用 postman
1.3、@RequestBody
@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的)
- @RequestBody直接以String接收前端传过来的json数据
后端代码 
    /**
     * @param stuSno 学号
     * @return 学生信息
     * @description 根据主键获取学生信息
     */
    @GetMapping("/selectByPrimaryKey3")
    public Student selectByPrimaryKey2(@RequestBody String jsonString) {
        // 使用fastjson解析json格式字符串为json对象
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        // 获取学号
        String stuSno = jsonObject.getString("stuSno");
        return studentService.selectByPrimaryKey(stuSno);
    }
前端postman
 需要通过fastjson转换json字符串为json对象从而获取相应的值,否则报错
- @RequestBody以简单对象接收前端传过来的json数据
实体类 
package com.geekmice.springbootrequestparam.pojo;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.io.Serializable;
import java.util.Date;
public class Student implements Serializable {
    private String stuSno;
    private String stuName;
    private String stuBorn;
    private String stuSex;
    public String getStuSno() {
        return stuSno;
    }
    public void setStuSno(String stuSno) {
        this.stuSno = stuSno;
    }
    public String getStuName() {
        return stuName;
    }
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }
    public String getStuBorn() {
        return stuBorn;
    }
    public void setStuBorn(String stuBorn) {
        this.stuBorn = stuBorn;
    }
    public String getStuSex() {
        return stuSex;
    }
    public void setStuSex(String stuSex) {
        this.stuSex = stuSex;
    }
    @Override
    public String toString() {
        return "Student{" +
                "stuSno='" + stuSno + '\'' +
                ", stuName='" + stuName + '\'' +
                ", stuBorn='" + stuBorn + '\'' +
                ", stuSex='" + stuSex + '\'' +
                '}';
    }
}
dao层
    /**
     * @param student 学生信息
     * @return 返回学生信息
     * @description 根据学生对象获取学生信息
     */
    List<Student> selectByPrimaryKeySelective(Student student);
xml
<!--获取学生信息-->
    <select id="selectByPrimaryKeySelective" resultType="student" parameterType="student">
        select
        <include refid="Base_Column_List"/>
        from student
        <where>
            <if test="stuSno != '' and stuSno != null">
                stu_sno = #{stuSno}
            </if>
            <if test="stuName != '' and stuName != null">
                and stu_name = #{stuName}
            </if>
            <if test="stuBorn != '' and stuBorn != null">
                and stu_born = #{stuBorn}
            </if>
            <if test="stuSex != '' and stuSex != null">
                and stu_sex = #{stuSex}
            </if>
        </where>
    </select>
service
    /**
     * @description 根据学生对象获取学生信息
     * @param student 学生信息
     * @return 返回学生信息
     */
    List<Student> selectByPrimaryKeySelective(Student student);
    @Override
    public List<Student> selectByPrimaryKeySelective(Student student) {
        return studentDao.selectByPrimaryKeySelective(student);
    }
controller
    /**
     * @param student 学生对象
     * @return 获取对应学生信息
     * @description 用户选择获取对应的学生信息
     */
    @PostMapping("/selectByPrimaryKeySelective")
    public List<Student> selectByPrimaryKeySelective(@RequestBody Student student) {
        return studentService.selectByPrimaryKeySelective(student);
    }
postman效果
- @RequestBody以复杂对象接收前端传过来的json数据
复杂对象:Tim 
package com.geekmice.springbootrequestparam.pojo;
import java.util.List;
/**
 * @author pmb
 * @create 2021-08-15-4:34
 */
public class Tim {
    // 团队id
    private Integer id;
    // 团队名字
    private String timName;
    // 获得荣誉
    private List<String> honors;
    // 团队成员
    private List<Student> studentList;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getTimName() {
        return timName;
    }
    public void setTimName(String timName) {
        this.timName = timName;
    }
    public List<String> getHonors() {
        return honors;
    }
    public void setHonors(List<String> honors) {
        this.honors = honors;
    }
    public List<Student> getStudentList() {
        return studentList;
    }
    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }
    @Override
    public String toString() {
        StringBuffer stringHonor = new StringBuffer("荣誉开始:。。。");
        for (String str : honors) {
            stringHonor.append(str);
            stringHonor.append("+");
        }
        StringBuffer stringTim = new StringBuffer("团队成员开始:。。。");
        for (Student student : studentList) {
            stringTim.append(student);
            stringTim.append("-");
        }
        stringHonor.append(stringTim);
        return stringHonor.toString();
    }
}

 postman
- @RequestBody与简单的@RequestParam()同时使用
 
controller
postman
- @RequestBody与复杂的@RequestParam()同时使用
controller
 
postman
- @RequestBody接收请求体中的json数据;不加注解接收URL中的数据并组装为对象
 

3、不同之处&应用场景
我认为在单个参数提交 API 获取信息的时候,直接放在 URL 地址里,也就是使用 URI 模板的方式是非常方便的,而不使用 @PathVariable 还需要从 request 里提取指定参数,多一步操作,所以如果提取的是多个参数,而且是多个不同类型的参数,我觉得应该使用其他方式,也就是 @RequestParam







