1.@RequestHeader简介
在熟悉项目的时候,遇到一个问题:用户登录后会获得一个唯一的uuid。在进行登陆后的其他操作时,都会先获取这个唯一id来证明用户是登陆状态。我在阅读代码时,发现这个标识(下面用loginCode来代表)都是通过@RequestHeader String loginCode来获取的。然而,我并不理解RequestHeader(我承认我大学是混子)。经过一番百度(其实是Bing),我大致了解到:
@RequestHeader注解是Spring框架中的一个注解,用于从HTTP请求头中获取特定的信息。通过使用@RequestHeader注解,我们可以方便地获取HTTP请求头中的各种信息,如用户代理、授权信息、内容类型等。
@RequestHeader注解可以用于方法的参数或方法的参数注解上。当用于方法的参数时,它将绑定HTTP请求头的值到方法参数上;当用于方法的参数注解时,它将绑定特定请求头的值到被注解的参数上。
HTTP请求报文的结构
一个HTTP请求报文的例子
2. @RequestHeader注解的使用方法
2.1 用于方法参数首先,我们来看一下@RequestHeader注解在方法参数上的使用方法。假设我们有一个处理HTTP请求的控制器类,并且需要获取User-Agent请求头的值。我们可以在方法参数上使用@RequestHeader注解来实现:
@RestController
public class UserController {
@GetMapping("/user-agent")
public String getUserAgent(@RequestHeader("User-Agent") String userAgent) {
return "User Agent: " + userAgent;
}
}
在上面的示例中,我们在getUserAgent方法的参数上使用了@RequestHeader(“User-Agent”)注解。这样,Spring Boot将自动将User-Agent请求头的值绑定到userAgent参数上,并返回包含该值的字符串。
2.2用于方法参数注解
除了直接在方法参数上使用@RequestHeader注解,我们还可以将它用于方法参数注解上。这样,我们可以将不同的请求头值绑定到不同的参数上,提高代码的可读性和灵活性。
@RestController
public class UserController {
@GetMapping("/user-info")
public String getUserInfo(@RequestHeader("User-Agent") String userAgent,
@RequestHeader("Accept-Language") String acceptLanguage) {
return "User Agent: " + userAgent + ", Accept Language: " + acceptLanguage;
}
}
在上面的示例中,我们使用了两个@RequestHeader注解,分别绑定了User-Agent和Accept-Language请求头的值到不同的参数上。
3.个人疑惑
知道了HTTP请求报文header携带的数据是如何拿到的,但我疑惑这些数据是怎么存储在header上的,即实现把数据存储在header上的代码是怎样的。于是我翻找项目源码发现并没有找到我要的答案。我猜测,既然项目是前后端分离的,不在后端,那应该在前端。
于是,我先让GPT简单介绍一下Request Header给我。它是这样说的:
Your Name: You provide your name, so the librarian knows who wants the book. In an HTTP request, this is similar to providing details about your client, like your browser type and version.
Your Request: You tell the librarian which book you want or what you want to do with it (borrow, return, or ask a question). In an HTTP request, this is equivalent to specifying the web page you want to see or the action you want to perform, like “GET /homepage” or “POST /login.”
Additional Information: Sometimes, you might have special requests or questions. For instance, you might say, “Please give me the book in Spanish,” or “Is this book available for borrowing?” In an HTTP request, you can include additional details or questions as headers. These headers can contain various information like the type of data you can accept, language preferences, and more.
Cookies: If you’ve been to the library before, the librarian might remember you and your preferences, like your favorite reading spot. Similarly, websites can remember you using cookies, which are also sent in the request header.
So, in a nutshell, request headers are like the introduction and special requests you make when you visit a website or web service. They help the web server understand who you are, what you want, and how you’d like to receive it.
和2中没两样。
再问一个问题:
const url = 'https://example.com/api/some_endpoint';
const headers = new Headers();
headers.append('Custom-Header-Name', 'YourHeaderValue');
fetch(url, {
method: 'GET', // or 'POST', 'PUT', 'DELETE', etc.
headers: headers,
})
.then(response => response.json())
.then(data => {
// Handle the response data here
})
.catch(error => {
console.error('Error:', error);
});
好了,也就是说,是前端来处理header的存储,所以不是我后端的工作了。