我有一个可以使用自定义状态代码玩的小演示。
有趣的是,如果状态低于 200,例如 105、199 等,请求将始终卡在那里。但适用于任何大于 200 的状态,例如 209、789 等。
Http状态码注册表,引用https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
Spring boot: 1.5.4.RELEASE with embedded tomcat
Java: 8
Controller :
@RestController
public class DemoController {
@GetMapping("/hello")
public ResponseEntity get() {
return ResponseEntity.status(105).build();
}
}
谁能给我一个明确的解释吗?
我在这里创建了一个要点:https://gist.github.com/pengisgood/dbea1fcdc45c2bb5809871c7f020b800
更新:
我还创建了一个小演示来重现它: https://github.com/pengisgood/springboot-customize-status-code
更新:
运行curl -v localhost:8080/hello
后,我可以看到状态,但响应尚未完成。请参阅下面的 gif:
最佳答案
我也遇到了这个问题,发现不是 Spring 造成了这种行为。它是 Tomcat 。
curl -v --header "Expect: 100-continue" http://localhost:8080
像这样调用任何已配置的端点将返回一个不会终止请求的额外响应代码。
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0*
Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.56.1
> Accept: */*
> Expect: 100-continue
>
< HTTP/1.1 100
< HTTP/1.1 200
< Set-Cookie: JSESSIONID=9355141A10CF546E9A9A43F5A5C0B1A4; Path=/; HttpOnly
< Content-Type: text/html;charset=ISO-8859-1
< Content-Length: 58
< Date: Tue, 31 Jul 2018 17:27:52 GMT
<
{ [58 bytes data]
100 58 100 58 0 0 58 0 0:00:01 --:--:-- 0:00:01 82<html>
<body>
<h2>Hello Heroku!</h2>
</body>
</html>
* Connection #0 to host localhost left intact
注意HTTP/1.1 100
此回复来自此项目 https://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat没有 Spring 。如果我修改 HelloServlet
以包含响应代码 100,它就会挂起。
更深入地观察: https://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.3
规范明确指出 100 响应应该发生在同一请求中。它挂起的原因是因为它期望客户端响应请求的内容。
查看 wiki 上的其他 1XX 响应代码,似乎也确实返回了一些信息而没有关闭请求。我的猜测是 Tomcat 希望所有 1xx 响应代码都以这种方式运行。
关于java - 当返回 1xx 状态代码时,Spring Boot 请求卡在那里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46283086/