如何在Spring MVC REST控制器中访问HTTP头信息? [英] How to get access to HTTP header information in Spring MVC REST controller?
问题描述
我是网络编程的新手,特别是在Java中,所以我刚学会了标题和正文。
I am new to web programming in general, especially in Java, so I just learned what a header and body is.
我正在使用Spring编写RESTful服务MVC。我能够在我的控制器中使用 @RequestMapping
创建简单的服务。我需要帮助了解如何从我的REST服务控制器中的方法请求中获取HTTP头信息。我想解析标题并从中获取一些属性。
I'm writing RESTful services using Spring MVC. I am able to create simple services with the @RequestMapping
in my controllers. I need help understanding how to get HTTP header information from a request that comes to my method in my REST service controller. I would like to parse out the header and get some attributes from it.
你能解释我如何获取这些信息吗?
Could you explain how I go about getting that information?
推荐答案
当您使用 @RequestHeader
注释参数时,该参数将检索标题信息。所以你可以这样做:
When you annotate a parameter with @RequestHeader
, the parameter retrieves the header information. So you can just do something like this:
@RequestHeader("Accept")
获取接受
标题。
所以从文档:
@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding,
@RequestHeader("Keep-Alive") long keepAlive) {
}
编码提供
Accept-Encoding
和 Keep-Alive
标头值分别是code>和 keepAlive
参数。
不用担心。我们都是新手。
And no worries. We are all noobs with something.
这篇关于如何在Spring MVC REST控制器中访问HTTP头信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!