The following annotations are included in the Spring Framework:@GetMapping
, @PostMapping
, @PutMapping
, @DeleteMapping
and @PatchMapping
for standard Spring MVC controller methods. However @HeadMapping
is not. What's the point about this?
In this quick article, we explained how to debug 404 errors in Spring MVC. We went through the two most common reasons for receiving a 404 response from our Spring application. The first was using an incorrect URI while making the request. The second was mapping the DispatcherServlet to the wrong url-pattern in web.
From Spring 5.2 onwards, the functional approach will also be available in the Spring Web MVC framework. As with the WebFlux module, RouterFunctions and RouterFunction are the main abstractions of this API.
What Is the Core Problem That Spring MVC Framework Solves? Spring MVC Framework provides decoupled way of developing web applications. With simple concepts like Dispatcher Servlet, ModelAndView and View Resolver, it makes it easy to develop web applications.
According to W3 Standard for HEAD Request
9.4 HEAD
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.
It is a Request Method that is similar to GET
but should not return a Body. Thus your GET
method will effectively handle the HEAD
method also but with the exception of not returning a response body.
So ideally you can use @GetMapping
to handle HEAD
request methods and can have a Filter
to avoid returning the response back to the calling client as discussed in this post
You could always fall back to the @RequestMapping
. This annotation supports all kind of HTTP methods. So @RequestMapping(method = { RequestMethod.HEAD })
does do the job!
If you really want to use @HeadMapping
, you can create it yourself:
@target({ ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = { RequestMethod.HEAD})
public @interface HeadMapping {
@AliasFor(annotation = RequestMapping.class)
String name() default "";
@AliasFor(annotation = RequestMapping.class)
String[] value() default {};
@AliasFor(annotation = RequestMapping.class)
String[] path() default {};
@AliasFor(annotation = RequestMapping.class)
String[] params() default {};
@AliasFor(annotation = RequestMapping.class)
String[] headers() default {};
@AliasFor(annotation = RequestMapping.class)
String[] consumes() default {};
@AliasFor(annotation = RequestMapping.class)
String[] produces() default {};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With