springboot中@RequestParam,@RequestBody,@PathVariable三个注解区别

三者都用于在Controller层接收前端传递的数据。

@RequestParam和@PathVariable

@PathVariable主要用于接收http://host:port/path/{参数值}数据。
@RequestParam主要用于接收http://host:port/path?参数名=参数值数据,这里后面也可以不跟参数值。
@RequestParam和@PathVariable这两者之间区别不大,主要是请求的URL不一样

例子:

//@PathVariable用法
@RequestMapping(value="getUserById/{userId}",method = RequestMethod.GET)
public Object getUserById(@PathVariable String userId)

//@RequestParam用法,注意这里请求后面没有添加参数
@RequestMapping(value="getUserById",method = RequestMethod.GET)
public Object getUserById(@RequestParam String userId) 

用@RequestParam请求接口时,URL是:http://www.test.com/user/getUserById?userId=1

用@PathVariable请求接口时,URL是:http://www.test.com/user/getUserById/1

所以为了实现RestFul的风格,会采用@PathVariable这种方式

关于@RequestParam:

@RequestMapping(value = "/test",method = RequestMethod.POST)
public Result test(@RequestParam(value="id",required=false,defaultValue="0")String id) 

注意上面@RequestParam用法当中的参数。

  • value表示接收数据的名称。
  • required表示接收的参数值是否必须,默认为true,既默认参数必须不为空,当传递过来的参数可能为空的时候可以设置required=false。
  • 此外还有一个参数defaultValue。表示如果此次参数未空则为其设置一个默认值。

@RequestParam和@RequestBody

RequestParam注解接收的参数是来自于url中,格式为xxx?username=123&password=456,而RequestBody注解接收的参数则是来自于requestBody中,即请求体中。

从Content-Type方面总结:

@RequestParam:
用来接收 Content-Type 是 application/x-www-form-urlencoded (这种格式的数据例如 user=1234&pwd=1234)编码的内容,这是浏览器默认的 content-Type(请求内容)格式。
@RequestBody:
用来处理不是 application/x-www-form-urlencoded 编码的内容例如:application/json application/xml

① form-data、x-www-form-urlencoded:不可以用@RequestBody;可以用@RequestParam。见postman的格局,这两种方式的时候没有json字符串部分。

② application/json:json字符串部分可以用@RequestBody;url中的?后面参数可以用@RequestParam。见postman的格局

使用示例:
例子一

//@RequestBody 满足要求的Content-Type时,接收参数为这两种情况适用

(@RequestBody Map map)
(@RequestBody Object object)

例子二

//@RequestParam 满足要求的Content-Type时,接收参数为这两种情况适用

(@RequestParam Map map)
(@RequestParam String waterEleId,@RequestParam String enterpriseName)

注意下面这种情况:
不管Content-Type为:application/json、form-data、x-www-form-urlencoded都不可用

(@RequestParam Object object)

从请求方式总结

POST请求时
@RequestBody --> JSON字符串部分
@RequestParam --> 请求参数部分

GET请求中不可以使用@RequestBody

而@RequestParam可用于各种类型的请求,例如:GET、POST、DELETE等请求。

SpringBoot传递单一参数时@RequestParam和@RequestBody的区别

参见链接

本文参考文档有:
https://blog.csdn.net/weixin_38004638/article/details/99655322
https://bbs.csdn.net/topics/392660349

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是你所需的代码。 Student.java: ```java public class Student { private int id; private String name; private int age; private int classId; private String sex; private int score; public Student(int id, String name, int age, int classId, String sex, int score) { this.id = id; this.name = name; this.age = age; this.classId = classId; this.sex = sex; this.score = score; } // Getters and setters public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getClassId() { return classId; } public void setClassId(int classId) { this.classId = classId; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } } ``` Main.java: ```java import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<Student> students = Arrays.asList( new Student(1, "Alice", 20, 1, "F", 90), new Student(2, "Bob", 18, 1, "M", 85), new Student(3, "Charlie", 19, 2, "M", 95), new Student(4, "David", 22, 2, "M", 88), new Student(5, "Eve", 21, 3, "F", 92) ); // 1. 返回仅age为偶数的Student列表 List<Student> evenAgeStudents = students.stream() .filter(student -> student.getAge() % 2 == 0) .collect(Collectors.toList()); System.out.println("Even age students: " + evenAgeStudents); // 2. 按score降序排列,仅返回前三个Student List<Student> topThreeStudents = students.stream() .sorted((s1, s2) -> s2.getScore() - s1.getScore()) .limit(3) .collect(Collectors.toList()); System.out.println("Top three students: " + topThreeStudents); // 3. 按照classId分组,返回Map<Integer,List<Student>>班级分组数据 Map<Integer, List<Student>> classIdToStudentsMap = students.stream() .collect(Collectors.groupingBy(Student::getClassId)); System.out.println("ClassId to students map: " + classIdToStudentsMap); // 使用反射获取Student类所有构造方法、成员方法、成员变量 Class<Student> studentClass = Student.class; Constructor<?>[] constructors = studentClass.getConstructors(); System.out.println("Constructors: " + Arrays.toString(constructors)); Method[] methods = studentClass.getMethods(); System.out.println("Methods: " + Arrays.toString(methods)); Field[] fields = studentClass.getDeclaredFields(); System.out.println("Fields: " + Arrays.toString(fields)); } } ``` 对于Spring Boot和MyBatis的部分,这里提供代码框架,你可以根据你的具体需求进行完善。 StudentMapper.java: ```java import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import java.util.List; @Mapper public interface StudentMapper { List<Student> getAllStudents(); Student getStudentById(int id); void insertStudent(Student student); void updateStudent(Student student); void deleteStudent(int id); } ``` StudentController.java: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/students") public class StudentController { @Autowired private StudentMapper studentMapper; @GetMapping("/") public List<Student> getAllStudents() { return studentMapper.getAllStudents(); } @GetMapping("/{id}") public Student getStudentById(@PathVariable int id) { return studentMapper.getStudentById(id); } @PostMapping("/") public void insertStudent(@RequestBody Student student) { studentMapper.insertStudent(student); } @PutMapping("/") public void updateStudent(@RequestBody Student student) { studentMapper.updateStudent(student); } @DeleteMapping("/{id}") public void deleteStudent(@PathVariable int id) { studentMapper.deleteStudent(id); } } ``` StudentApplication.java: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class StudentApplication { public static void main(String[] args) { SpringApplication.run(StudentApplication.class, args); } } ``` 这只是一个简单的示例,你可以根据自己的需求进行修改和扩展。关于拦截器打印每个接口用时的部分,你可以创建一个实现了HandlerInterceptor接口的拦截器类,并在其实现相应的逻辑。 这里只提供了代码框架,具体的实现需要根据你的项目需求进行完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值