Spring 3.0 HEAD 请求
最近我们转向了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在当前的 Spring (4.3.10) 中,自动支持 HEAD:
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:
https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-requestmapping-head-options
我相信这就是您正在寻找的:
http://www.axelfontaine.com/2009/09/transparently -支持-http-head.html
I believe this is what you're looking for:
http://www.axelfontaine.com/2009/09/transparently-supporting-http-head.html
只需添加
HEAD
作为请求映射支持的方法:更新:我认为您可以提供一个自定义类,该类扩展
AnnotationMethodHandlerAdapter
作为方法处理程序(在dispatcher -servlet.xml
),并绕过那里的 HEAD 支持检查。但我只是使用 IDE 的替换功能来添加它。Just add
HEAD
as a supported method the the request mapping:Update: I think you can provide a custom class that extends
AnnotationMethodHandlerAdapter
to be the method handler (indispatcher-servlet.xml
), and just bypass the HEAD support check there. But I'd just use the replace features of an IDE to add it.