springboot web 接口入参相关注解跟请求报文示例, 煎饼果子来一套, 加蛋

煎饼果子来一套, 加蛋 🏜️

真正爱你的人,不会总想把你变成他喜欢的样子,而是喜欢你每一个样子

入参相关注解就像煎饼果子, 哎呦, 还不够, 我还要请求报文, 加蛋
以下是常见的 Spring Boot Web 接口入参相关的注解:

  1. @RequestParam:获取请求参数,可以指定参数名和是否必填。例如:
@GetMapping("/hello")
public String sayHello(@RequestParam(name = "name", required = true) String name) {
    return "Hello, " + name + "!";
}

请求报文:/hello?name=John

  1. @PathVariable:获取 URL 路径中的参数。例如:
@GetMapping("/users/{userId}")
public User getUserById(@PathVariable Long userId) {
    return userService.getUserById(userId);
}

请求报文:/users/123

  1. @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": "******"
}
  1. @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...

  1. @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 接口入参相关的注解,可以根据实际需求进行选择。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值