Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is @HeadMapping unavailable in Spring MVC?

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?

like image 308
petertc Avatar asked Jul 13 '17 07:07

petertc


People also ask

Why does Spring MVC have 404 error?

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.

Is the functional API available for Spring MVC?

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 solve?

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.


2 Answers

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

like image 166
shazin Avatar answered Sep 21 '22 08:09

shazin


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 {};
}
like image 27
Jacob van Lingen Avatar answered Sep 19 '22 08:09

Jacob van Lingen