Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 3.0 HEAD Requests

Tags:

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.

like image 638
Janning Avatar asked Sep 27 '10 10:09

Janning


People also ask

Is Head request faster than get?

The HEAD method is used to ask only for information about a document, not for the document itself. HEAD is much faster than GET, as a much smaller amount of data is transferred. It's often used by clients who use caching, to see if the document has changed since it was last accessed.

What are head requests?

The HTTP HEAD method requests the headers that would be returned if the HEAD request's URL was instead requested with the HTTP GET method. For example, if a URL might produce a large download, a HEAD request could read its Content-Length header to check the filesize without actually downloading the file.

What is the difference between GET and HEAD request?

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.

Are head requests cached?

The response to a HEAD request MAY be cacheable in the sense that the information contained in the response MAY be used to update a previously cached entity from that resource. It doesn't really make sense to cache the response to a HEAD request itself, as it contains no entity.


2 Answers

I believe this is what you're looking for: http://www.axelfontaine.com/2009/09/transparently-supporting-http-head.html

like image 69
Nino Skilj Avatar answered Oct 12 '22 02:10

Nino Skilj


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

like image 35
Janning Avatar answered Oct 12 '22 02:10

Janning