SpringMvcAnnotation

SUGGESTION

Simplification

View source code on GitHub

Summary

Prefer the conciseness of @{Get,Put,Post,Delete,Patch}Mapping over @RequestMapping

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("SpringMvcAnnotation") to the enclosing element.

Disable this pattern completely by adding -Xep:SpringMvcAnnotation:OFF as compiler argument. Learn more.

Samples

Replacement

Shows the difference in example code before and after the bug pattern is applied.

 import static org.springframework.web.bind.annotation.RequestMethod.PATCH;
 import static org.springframework.web.bind.annotation.RequestMethod.POST;
 import static org.springframework.web.bind.annotation.RequestMethod.PUT;
 
+import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PatchMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.PutMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 
 interface A {
-  @RequestMapping(method = RequestMethod.GET)
+  @GetMapping()
   A simple();
 
-  @RequestMapping(path = "/foo/bar", method = POST)
+  @PostMapping(path = "/foo/bar")
   A prefixed();
 
-  @RequestMapping(
-      method = {RequestMethod.DELETE},
-      path = "/foo/bar")
+  @DeleteMapping(path = "/foo/bar")
   A suffixed();
 
-  @RequestMapping(
+  @PutMapping(
       path = "/foo/bar",
-      method = {PUT},
       consumes = {"a", "b"})
   A surrounded();
 
-  @RequestMapping(method = {PATCH})
+  @PatchMapping()
   A curly();
 }
 

Identification

Shows code lines which will (not) be flagged by this bug pattern.
A //BUG: Diagnostic contains: comment is placed above any violating line.

import static org.springframework.web.bind.annotation.RequestMethod.DELETE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.HEAD;
import static org.springframework.web.bind.annotation.RequestMethod.PATCH;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
import static org.springframework.web.bind.annotation.RequestMethod.PUT;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

interface A {
  @RequestMapping
  A simple();

  @RequestMapping(method = {})
  A explicitDefault();

  // BUG: Diagnostic contains:
  @RequestMapping(method = RequestMethod.GET)
  A get();

  // BUG: Diagnostic contains:
  @RequestMapping(method = {RequestMethod.POST})
  A post();

  // BUG: Diagnostic contains:
  @RequestMapping(method = {PUT})
  A put();

  // BUG: Diagnostic contains:
  @RequestMapping(method = {DELETE})
  A delete();

  // BUG: Diagnostic contains:
  @RequestMapping(method = {PATCH})
  A patch();

  @RequestMapping(method = HEAD)
  A head();

  @RequestMapping(method = RequestMethod.OPTIONS)
  A options();

  @RequestMapping(method = {GET, POST})
  A simpleMix();

  @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
  A verboseMix();

  @DeleteMapping
  A properDelete();

  @GetMapping
  A properGet();

  @PatchMapping
  A properPatch();

  @PostMapping
  A properPost();

  @PutMapping
  A properPut();
}

Copyright © 2017-2024 Picnic Technologies BV