我有一个使用以下控制器在本地主机上运行的Spring Boot应用程序:
@RestController
public class EtagController {
@GetMapping("/eTag")
public ResponseEntity<String> onGet() {
HttpHeaders headers = new HttpHeaders();
headers.set("ETag", UUID.randomUUID().toString());
return new ResponseEntity<>("", headers, HttpStatus.OK);
}
@PostMapping("/eTag")
public ResponseEntity<String> onPost() {
HttpHeaders headers = new HttpHeaders();
headers.set("ETag", UUID.randomUUID().toString());
return new ResponseEntity<>("", headers, HttpStatus.OK);
}
}
以及浏览器中运行的以下html / javascript:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<script>
function onGet() {
var xhr = new XMLHttpRequest()
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
console.log('Success');
}
}
xhr.onerror = function() {
console.log('Failure');
}
xhr.open('Get', 'http://127.0.0.1:8080/eTag', true)
xhr.send()
}
function onPost() {
var xhr = new XMLHttpRequest()
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
console.log('Success');
}
}
xhr.onerror = function() {
console.log('Failure');
}
xhr.open('Post', 'http://127.0.0.1:8080/eTag', true)
xhr.send()
}
</script>
<button id="get" onclick="onGet()">Do Get</button>
<button id="post" onclick="onPost()">Do Post</button>
</body>
</html>
当我在初始请求之后发出Get
个请求时,将包含If-None-Match
标头,但即使在第一个请求之后,我执行Post
个请求时也不会包含标头。据我所知,spec中没有任何内容暗示ETags
和If-None-Match
是Get
专有的。我必须自己添加标头,还是可以通过某种方式在Post
请求中强制执行标头?如果是这样,我将如何访问缓存的Etag?