Why is @HeadMapping unavailable in Spring MVC?

Viewed 3621

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?

2 Answers

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 {};
}
Related