代码之家  ›  专栏  ›  技术社区  ›  Max Peng

返回1xx状态代码时,弹簧引导请求挂起

  •  2
  • Max Peng  · 技术社区  · 8 年前

    Http状态代码注册表,请参阅 https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml

    爪哇:8

    控制器:

    @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:

    curl result

    2 回复  |  直到 5 年前
        1
  •  1
  •   Tiris    7 年前

    我也遇到了这个问题,发现不是Spring造成了这种行为。它是雄猫。

    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 它没有春天。如果我修改 HelloServlet 要包含100的响应代码,它只是挂起。

    深入观察: https://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.3

    查看wiki中的其他1XX响应代码,似乎也确实返回了一些信息,但没有关闭请求。我猜想Tomcat希望所有1xx响应代码都以这种方式工作。

        2
  •  1
  •   Phil Webb    8 年前

    据我所知,春天 DispacherServlet 1xx

    This article 为状态代码提供了良好的入门知识。这句话特别相关:

    100中的100199代码是信息性的,表示客户应以其他一些操作进行响应。

    如果你跑步 curl 具有 --trace 你会看到 105

    curl -v -trace http://localhost:8080/hello                                                                                                                                                                                                                                                                                                 
    Trying ::1...
    TCP_NODELAY set
    Connected to localhost (::1) port 8080 (#0)
    > GET /hello HTTP/1.1
    > Host: localhost:8080
    > User-Agent: curl/7.54.0
    > Accept: */*
    > 
    < HTTP/1.1 105 
    < Date: Tue, 19 Sep 2017 18:07:04 GMT
    ^C
    

    因此,我认为正在发生的是,返回了响应,客户端应该用一些其他操作进行响应(但没有),因此看起来事情已经挂起了。

    105 状态和你预计会发生什么?