Spring 3.0 HEAD 请求 [英] Spring 3.0 HEAD Requests

查看:59
本文介绍了Spring 3.0 HEAD 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我们转向 spring 3.0 控制器处理,如下所示:

recently we moved to spring 3.0 Controller handling like this:

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

现在,由于 HEAD 请求,我们的日志中出现了大量异常.

Now we are getting lots of Exceptions in our logs because of HEAD Requests.

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)
    ...

我想像 GET 请求一样支持 HEAD 请求,但当然要遵守 HTTP 参考:

I would like to support HEAD Requests the same way like GET Requests, but obeying the HTTP reference of course:

HEAD 方法与 GET 相同除了服务器不得
在响应中返回消息正文.包含的元信息响应 HEAD 的 HTTP 标头请求应该与响应 GET 发送的信息要求.这个方法可以用用于获取有关的元信息请求隐含的实体不转移实体主体本身.这种方法经常使用用于测试超文本链接有效性、可访问性和最近修改.http://www.ietf.org/rfc/rfc2616.txt

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

有没有人有一个优雅的解决方案,或者甚至有一个开箱即用的 Spring 解决方案?

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.

推荐答案

在当前的Spring(4.3.10)中自动支持HEAD:

In the current Spring (4.3.10) HEAD is automatically supported:

映射到GET"的@RequestMapping 方法也隐式映射到HEAD",即不需要显式声明HEAD".一个HTTP HEAD 请求被当作 HTTP GET 处理,除了而不是写入正文只计算字节数和内容长度"标头集.

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

https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-requestmapping-head-options

这篇关于Spring 3.0 HEAD 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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