Spring 3.0 HEAD 请求

发布于 2024-09-25 09:12:47 字数 1419 浏览 2 评论 0原文

最近我们转向了 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
   }
}

现在,由于 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)
    ...

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

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

有没有人有优雅的解决方案还是有开箱即用的弹簧解决方案?

我在网上搜索但没有找到任何答案。

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

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独木成林 2024-10-02 09:12:47

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

映射到“GET”的 @RequestMapping 方法也隐式映射到
“HEAD”,即不需要显式声明“HEAD”。一个
HTTP HEAD 请求的处理方式就像 HTTP GET 一样,除了
只计算字节数,而不是写入正文
“Content-Length”标头集。

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

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

@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

谁与争疯 2024-10-02 09:12:47

只需添加 HEAD 作为请求映射支持的方法:

@RequestMapping(method = {RequestMethod.GET, RequestMethod.HEAD})

更新:我认为您可以提供一个自定义类,该类扩展 AnnotationMethodHandlerAdapter 作为方法处理程序(在 dispatcher -servlet.xml),并绕过那里的 HEAD 支持检查。但我只是使用 IDE 的替换功能来添加它。

Just add HEAD as a supported method the the request mapping:

@RequestMapping(method = {RequestMethod.GET, RequestMethod.HEAD})

Update: I think you can provide a custom class that extends AnnotationMethodHandlerAdapter to 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文