springboot 请求添加header
时间: 2023-11-22 20:28:47 浏览: 33
在 Spring Boot 中,可以使用 @HeaderParam 注解来获取请求头中的信息,并且使用 @RequestHeader 注解来设置请求头中的信息。下面是一个示例代码,演示如何在 Spring Boot 中添加请求头:
```java
@RestController
@RequestMapping("/api")
public class ApiController {
@GetMapping("/example")
public String example(@RequestHeader("Authorization") String authorization) {
// 处理请求
return "Hello, " + authorization;
}
@PostMapping("/example")
public String examplePost(@RequestBody String requestBody, @RequestHeader("Content-Type") String contentType) {
// 处理请求
return "Content-Type: " + contentType + ", Request Body: " + requestBody;
}
}
```
在上面的代码中,我们使用了 @RequestHeader 注解来获取请求头中的 Authorization 和 Content-Type 字段的值,并且使用了 @PostMapping 注解来设置请求头中的 Content-Type 字段的值。你可以根据自己的需求来调整代码。