java 如何在 Spring MVC 中处理 HTTP 标头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13882858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:19:46  来源:igfitidea点击:

How to handle HTTP headers in Spring MVC?

javaspringspring-mvchttp-headershttprequest

提问by AndreaNobili

in this period I am studying the Spring MVC showcase example. Now I am studying how Spring MVC requires data.

这期间我在研究Spring MVC的展示示例。现在我正在研究 Spring MVC 如何需要数据。

I have some difficulties to understand how HTTP Request header are handled.

我在理解 HTTP 请求标头的处理方式方面遇到了一些困难。

In pratcice I have the following link:

在实践中,我有以下链接:

<a id="header" class="textLink" href="<c:url value="/data/header" />">Header</a>

This link generate an HTTP Request towards the URL: "/data/header"

此链接生成一个指向 URL 的 HTTP 请求:“/data/header”

This is the method of the RequestDataController class that handles this HTTP Request (the entire class is annoted by @RequestMapping("/data"): so this method handle /data/header URL)

这是RequestDataController类处理这个HTTP请求的方法(整个类用@RequestMapping("/data")注解:所以这个方法处理/data/header URL)

@RequestMapping(value="header", method=RequestMethod.GET)
public @ResponseBody String withHeader(@RequestHeader String Accept) {
    return "Obtained 'Accept' header '" + Accept + "'";
}

So the withHeader method take a parameter that is annoted by @RequestHeaderannotation that is an annotation which indicates that a method parameter should be bound to a web request header.

因此 withHeader 方法采用一个由@RequestHeader注释注释的参数,该注释指示方法参数应绑定到 Web 请求标头。

Ok, so my answer is: what exactly I have inside the Accept variable? The value of my HTTP Accetp Header? or what?

好的,所以我的答案是:我在 Accept 变量中到底有什么?我的 HTTP Accetp 标头的值?要不然是啥?

Fow what I know Accept request-header field can be used to specify certain media types which are acceptable for the response. Accept headers can be used to indicate that the request is specifically limited to a small set of desired types, as in the case of a request for an in-line image.

我所知道的 Accept 请求头字段可用于指定响应可接受的某些媒体类型。Accept 标头可用于指示请求仅限于一小组所需的类型,如请求内嵌图像的情况。

So my output (the returned value) is: Obtained 'Accept' header 'text/plain, /; q=0.01'

所以我的输出(返回值)是:获得 'Accept' header 'text/plain, /; q=0.01'

mmm...what it exactly means this Accept headers field value?

嗯……这个 Accept headers 字段值到底是什么意思?

Thank you

谢谢

Andrea

安德烈亚

回答by Biju Kunjummen

Yes, when you annotate a parameter with @RequestHeader, the parameter name is used for retrieving the header information - in your case that will be the header name of 'Accept'- the parameter name could have been acceptalso, the header names are retrieved in a case insensitive manner.

是的,当您使用@RequestHeader 注释参数时,参数名称用于检索标头信息-在您的情况下,这将是“接受”的标头名称-参数名称也可能是accept,标头名称在不区分大小写的方式。

You could have also explicitly specified the header name explicitly this way: @RequestHeader("Accept")

您还可以通过这种方式明确指定标头名称: @RequestHeader("Accept")

Accept header like you have indicated is a way for the client(browser) to say what it can accept as a media type of the response to be.

像您指出的那样接受标头是客户端(浏览器)说出它可以接受的响应媒体类型的一种方式。