Spring 3.0 HEAD 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3803015/
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
Spring 3.0 HEAD Requests
提问by Janning
recently we moved to spring 3.0 Controller handling like this:
最近我们转向 spring 3.0 控制器处理,如下所示:
@Controller
public class MyController {
@RequestMapping(method = RequestMethod.POST)
protected String onSubmit ( Form form, Errors errors) {
// handle POST
}
@RequestMapping(method = RequestMethod.GET)
protected void getForm ( Form form ) {
// handle GET
}
}
Now we are getting lots of Exceptions in our logs because of HEAD Requests.
现在,由于 HEAD 请求,我们在日志中收到了很多异常。
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'HEAD' not supported
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodResolver.resolveHandlerMethod(AnnotationMethodHandlerAdapter.java:621)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:422)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415)
...
I would like to support HEAD Requests the same way like GET Requests, but obeying the HTTP reference of course:
我想以与 GET 请求相同的方式支持 HEAD 请求,但当然要遵守 HTTP 参考:
The HEAD method is identical to GET except that the server MUST NOT
return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification. http://www.ietf.org/rfc/rfc2616.txt
HEAD 方法与 GET 相同,除了服务器不得
在响应中返回消息正文。包含在响应 HEAD 请求的 HTTP 标头中的元信息应该与响应 GET 请求发送的信息相同。此方法可用于获取有关请求所隐含的实体的元信息,而无需传输实体主体本身。这种方法通常用于测试超文本链接的有效性、可访问性和最近的修改。 http://www.ietf.org/rfc/rfc2616.txt
Does anybody has an elegant solution or is there even a spring solution out-of-the-box?
有没有人有一个优雅的解决方案,或者甚至有一个开箱即用的弹簧解决方案?
I searched the web but did not find any answers to this.
我在网上搜索,但没有找到任何答案。
采纳答案by Janning
In the current Spring (4.3.10) HEAD is automatically supported:
在当前的 Spring (4.3.10) 中自动支持 HEAD:
@RequestMapping methods mapped to "GET" are also implicitly mapped to "HEAD", i.e. there is no need to have "HEAD" explicitly declared. An HTTP HEAD request is processed as if it were an HTTP GET except instead of writing the body only the number of bytes are counted and the "Content-Length" header set.
映射到“GET”的@RequestMapping 方法也隐式映射到“HEAD”,即不需要显式声明“HEAD”。处理 HTTP HEAD 请求就像处理 HTTP GET 一样,除了不写入正文,只计算字节数和设置“Content-Length”标头。
回答by Nino Skilj
I believe this is what you're looking for: http://www.axelfontaine.com/2009/09/transparently-supporting-http-head.html
我相信这就是你要找的:http: //www.axelfontaine.com/2009/09/transparently-supporting-http-head.html
回答by Bozho
Just add HEADas a supported method the the request mapping:
只需将HEAD请求映射添加为受支持的方法:
@RequestMapping(method = {RequestMethod.GET, RequestMethod.HEAD})
Update: I think you can provide a custom class that extends AnnotationMethodHandlerAdapterto be the method handler (in dispatcher-servlet.xml), and just bypass the HEAD support check there. But I'd just use the replace features of an IDE to add it.
更新:我认为您可以提供一个自定义类,该类扩展AnnotationMethodHandlerAdapter为方法处理程序(在 中dispatcher-servlet.xml),并且只需绕过那里的 HEAD 支持检查。但我只是使用 IDE 的替换功能来添加它。

