煎饼果子来一套, 加蛋 🏜️
真正爱你的人,不会总想把你变成他喜欢的样子,而是喜欢你每一个样子
入参相关注解就像煎饼果子, 哎呦, 还不够, 我还要请求报文, 加蛋
以下是常见的 Spring Boot Web 接口入参相关的注解:
- @RequestParam:获取请求参数,可以指定参数名和是否必填。例如:
@GetMapping("/hello")
public String sayHello(@RequestParam(name = "name", required = true) String name) {
return "Hello, " + name + "!";
}
请求报文:/hello?name=John
- @PathVariable:获取 URL 路径中的参数。例如:
@GetMapping("/users/{userId}")
public User getUserById(@PathVariable Long userId) {
return userService.getUserById(userId);
}
请求报文:/users/123
- @RequestBody:获取请求体中的参数。例如:
@PostMapping("/users")
public User createUser(@RequestBody User user) {
return userService.createUser(user);
}
请求报文:
POST /users HTTP/1.1
Content-Type: application/json
{
"username": "john",
"word": "******"
}
- @RequestHeader:获取请求头中的参数。例如:
@GetMapping("/hello")
public String sayHello(@RequestHeader(name = "User-Agent") String userAgent) {
return "Hello, your user agent is " + userAgent + ".";
}
请求报文:GET /hello HTTP/1.1\r\nUser-Agent: Mozilla/5.0...
- @CookieValue:获取请求中的 Cookie。例如:
@GetMapping("/hello")
public String sayHello(@CookieValue(name = "sessionId") String sessionId) {
return "Hello, your session ID is " + sessionId + ".";
}
请求报文:GET /hello HTTP/1.1\r\nCookie: sessionId=abc123
以上是常见的 Spring Boot Web 接口入参相关的注解,可以根据实际需求进行选择。