# 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;
}

Use a Java Refactoring Tool

No license required

You can review this refactoring on your code without a license by installing jSparrow to your Eclipse IDE. Install the plug-in from Eclipse IDE: Eclipse Marketplace.

System-wide Refactoring

Do you want to automate this refactoring (and many more) to your system-wide code? The automatic application of this system-wide refactoring can be unlocked by acquiring your jSparrow license.

a drawn cute bird pointing at a graph that shows positive results

# Properties

Property Value
Rule ID ReplaceRequestMappingAnnotation
First seen in jSparrow version 4.12.0
Minimum Java version 5
Remediation cost 2 min
Links