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

查看:674
本文介绍了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
请求时发送的
信息相同。这个方法可以用
来获取关于
的元信息,这个信息是由
请求隐含的实体而不转移entity-body
本身。这种方法通常用
来测试超文本链接的
有效性,可访问性和最近的
修改。
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.

推荐答案

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

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


映射到GET的@RequestMapping方法也隐式映射到
HEAD,即不需要显式声明HEAD。
HTTP HEAD请求被处理,好像它是一个HTTP GET,除了
而不是写主体只计算字节数,
是Content-Length标头集。

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

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

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

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