Spring Boot 1.4.0:REST 控制器不设置 HTTP 响应的原因 [英] Spring Boot 1.4.0 : REST Controllers don't set reason to HTTP response

查看:30
本文介绍了Spring Boot 1.4.0:REST 控制器不设置 HTTP 响应的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Spring Tool SuiteSpring Starter Project"创建了 2 个项目:

I have 2 projects created with Spring Tool Suite "Spring Starter Project":

第一个项目: Spring Boot 1.3.7(发布版)+ Web

First project : Spring Boot 1.3.7 (Release) + Web

第二个项目: Spring Boot 1.4.0 (Release) + Web

Second project : Spring Boot 1.4.0 (Release) + Web

我将每个项目的 @SpringBootApplication 类替换为以下代码:

I replace the @SpringBootApplication class of each project with this code:

@SpringBootApplication
@RestController
public class TestWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestWebApplication.class, args);
    }

    @RequestMapping("/hello")
    public String sayHello() {
        return "hello!";
    }
}

当我在每台服务器上调用 GET/hello 时,我得到以下响应:

When I call GET /hello on each server I get the following responses:

第一个项目(1.3.7):状态 200 (无理由)

第二个项目 (1.4.0): 状态 200 正常

这是一个错误还是我遗漏了什么?

Is it a bug or am I missing something?

此外,我无法为 1.4.0 项目设置理由.

Besides, I can't set a reason to the 1.4.0 project.

推荐答案

行为的变化是由于使用 Tomcat 8.5 的 Spring Boot 1.4 和使用 Tomcat 8.0 的 Spring Boot 1.3.Tomcat 8.5 不发送原因短语.

The change in behaviour is due to Spring Boot 1.4 using Tomcat 8.5 and Spring Boot 1.3 using Tomcat 8.0. Tomcat 8.5 doesn't send a reason phrase.

严格来说,不需要理由短语.在 RFC 7230 中,原因短语定义为:

Strictly speaking, the reason phrase isn't required. In RFC 7230 the reason phrase is defined as:

reason-phrase  = *( HTAB / SP / VCHAR / ons-text )

其中 * 表示零个或多个.换句话说,原因短语为空是合法的.

Where * means zero or more. In other words it's legal for the reason phrase to be empty.

Tomcat 8.5 利用这一点,通过删除原因短语来节省一些带宽.如果这给您的客户带来了问题,那么它可以说不符合规范,因为上面链接的 RFC 部分是这样说的:

Tomcat 8.5 takes advantage of this and saves some bandwidth by dropping the reason phrase. If this is causing a problem for your client, then it arguably isn't spec compliant as the section of the RFC linked to above has this to say:

原因短语元素的唯一目的是提供与数字状态代码相关的文本描述,主要是出于对早期互联网应用协议的尊重更常用于交互式文本客户端.客户应该忽略原因短语内容.

The reason-phrase element exists for the sole purpose of providing a textual description associated with the numeric status code, mostly out of deference to earlier Internet application protocols that were more frequently used with interactive text clients. A client SHOULD ignore the reason-phrase content.

您还没有说明原因短语的缺失是否会导致问题.如果不是,那么您最好的做法就是不要担心并继续前进.如果您出于某种原因真的想保留原因短语,那么切换到 Undertow 或 Jetty 是您最好的选择.

You haven't said if the absence of the reason phrase is causing a problem. If it's not, then your best course of action is not to worry about it and to move on. If you really want to keep the reason phrase for some reason then switching to Undertow or Jetty is your best bet.

这篇关于Spring Boot 1.4.0:REST 控制器不设置 HTTP 响应的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆