# Replace Request Mapping Annotation
# Description
The Spring Framework 4.3 introduced some composed annotations like @GetMapping
, @PostMapping
, etc... as an alternative of @RequestMapping(method=...)
(opens new window) for annotating HTTP request handlers.
Accordingly, this rule replaces the @RequestMapping
annotations with their equivalent dedicated alternatives, for example, @RequestMapping(value = "/hello", method = RequestMethod.GET)
is replaced by @GetMapping(value = "/hello")
.
Annotation | Request Method |
---|---|
@GetMapping (opens new window) | RequestMethod.GET |
@PostMapping (opens new window) | RequestMethod.POST |
@PutMapping (opens new window) | RequestMethod.PUT |
@PatchMapping (opens new window) | RequestMethod.PATCH |
@DeleteMapping (opens new window) | RequestMethod.DELETE |
Requirements
This rule requires the following library to be present:
- Spring Web 4.3.5 or later
# Benefits
Applying this rule removes unnecessary code.
# Tags
# Code Changes
# Replacement by @GetMapping
Pre
@RequestMapping(value = "/users/get/{userId}", method = RequestMethod.GET)
public @ResponseBody ResourceBean getUser(@PathVariable String userId,
HttpServletRequest request) {
ResourceBean responseBean;
// determine responseBean...
return responseBean;
}
Post
@GetMapping(value = "/users/get/{userId}")
public @ResponseBody ResourceBean getUser(@PathVariable String userId,
HttpServletRequest request) {
ResourceBean responseBean;
// determine responseBean...
return responseBean;
}
# Replacement by @PostMapping
Pre
@RequestMapping(value = "/users", method = RequestMethod.POST)
public @ResponseBody ResourceBean addUser(@RequestBody UserBean userBean,
HttpServletRequest request) {
ResourceBean responseBean;
// determine responseBean...
return responseBean;
}
Post
@PostMapping(value = "/users")
public @ResponseBody ResourceBean addUser(@RequestBody UserBean userBean,
HttpServletRequest request) {
ResourceBean responseBean;
// determine responseBean...
return responseBean;
}
# Replacement by @PutMapping
Pre
@RequestMapping(value = "/users", method = RequestMethod.PUT)
public @ResponseBody ResourceBean updateUser(@RequestBody UserBean userBean,
HttpServletRequest request) {
ResourceBean responseBean;
// determine responseBean...
return responseBean;
}
Post
@PutMapping(value = "/users")
public @ResponseBody ResourceBean updateUser(@RequestBody UserBean userBean,
HttpServletRequest request) {
ResourceBean responseBean;
// determine responseBean...
return responseBean;
}
# Replacement by @PatchMapping
Pre
@RequestMapping(value = "/users", method = RequestMethod.PATCH)
public @ResponseBody ResourceBean updateUser(@RequestBody UserBean userBean,
HttpServletRequest request) {
ResourceBean responseBean;
// determine responseBean...
return responseBean;
}
Post
@PatchMapping(value = "/users")
public @ResponseBody ResourceBean updateUser(@RequestBody UserBean userBean,
HttpServletRequest request) {
ResourceBean responseBean;
// determine responseBean...
return responseBean;
}
# Replacement by @DeleteMapping
Pre
@RequestMapping(value = "/users/{userId}", method = RequestMethod.DELETE)
public @ResponseBody ResourceBean deleteUser(@PathVariable String userId,
HttpServletRequest request) {
ResourceBean responseBean;
// determine responseBean...
return responseBean;
}
Post
@DeleteMapping(value = "/users/{userId}")
public @ResponseBody ResourceBean deleteUser(@PathVariable String userId,
HttpServletRequest request) {
ResourceBean responseBean;
// determine responseBean...
return responseBean;
}
🛠️ Auto-refactor Available
You can auto-refactor this with jSparrow.
Drop this button to your Eclipse IDE workspace to install jSparrow for free:
Need help? Check out our installation guide.