how to change the default Controller Name in Swagger Spring MVC/REST

This tutorial shows how to change the default Controller name in swagger-ui/api docs generated by the swagger/springfox-swagger-ui/springfox-swagger2 for Spring REST Controllers/APIs

Spring REST Controller

Following is the sample Employee REST Controller (Class Name : EmployeeRestController)

@RequestMapping("/rs/employee")
@RestController
public class EmployeeRestController {


	@RequestMapping(value = "/{employeeId}", method = RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<Employee> getEmployee(@PathVariable("employeeId") String employeeId) {
                          :
		return new ResponseEntity<Employee>(employee, HttpStatus.OK);
	}

	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<Employee> createEmployee(@RequestBody Employee employee) {
                          :
		return new ResponseEntity<Employee>(savedEmployee, HttpStatus.OK);
	}
	
	@RequestMapping(method = RequestMethod.PUT)
	@ResponseBody
	public ResponseEntity<Employee> updateEmployee(@RequestBody Employee employee) {
                      :
		return new ResponseEntity<Employee>(upudatedEmployee, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/{employeeId}", method = RequestMethod.DELETE)
	@ResponseBody
	public ResponseEntity<Void> deleteEmployee(@PathVariable("employeeId") String employeeId) {
                      :
		return new ResponseEntity<Void>(HttpStatus.OK);
	}
	
	@RequestMapping(value = "/list/{page}/{size}", method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<Page&>lt;Employee> listEmployees(@RequestBody Employee findBy, 
				@PathVariable("page") int page, @PathVariable("size") int size) {
                      :
		return new ResponseEntity<Page&>lt;Employee>(employeePaginatedList, HttpStatus.OK);
	}
}

Default generated swagger-ui/api docs

By default swagger-fox api creates API with name as employee-rest-controller and description as Employee Rest Controller as shown below (highlighted in yellow)

Tags attribute in @Api and @ApiOperation annotations

We can customize the name and description of the REST API and methods by using the tags attribute as show below

@RequestMapping("/rs/employee")
@RestController
@Api(value = "Employee", description = "REST API for Employee", tags = { "Employee" })
public class EmployeeRestController {


	@ApiOperation(value="Get Employee", tags = { "Employee" })
	@RequestMapping(value = "/{employeeId}", method = RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<Employee> getEmployee(@PathVariable("employeeId") String employeeId) {
                          :
		return new ResponseEntity<Employee>(employee, HttpStatus.OK);
	}

	@ApiOperation(value="Create Employee", tags = { "Employee" })
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<Employee> createEmployee(@RequestBody Employee employee) {
                          :
		return new ResponseEntity<Employee>(savedEmployee, HttpStatus.OK);
	}
	
	@ApiOperation(value="Update Employee", tags = { "Employee" })
	@RequestMapping(method = RequestMethod.PUT)
	@ResponseBody
	public ResponseEntity<Employee> updateEmployee(@RequestBody Employee employee) {
                      :
		return new ResponseEntity<Employee>(upudatedEmployee, HttpStatus.OK);
	}
	
	@ApiOperation(value="Delete Employee", tags = { "Employee" })
	@RequestMapping(value = "/{employeeId}", method = RequestMethod.DELETE)
	@ResponseBody
	public ResponseEntity<Void> deleteEmployee(@PathVariable("employeeId") String employeeId) {
                      :
		return new ResponseEntity<Void>(HttpStatus.OK);
	}
	
	@ApiOperation(value="Get paginated list of Employees", tags = { "Employee" })
	@RequestMapping(value = "/list/{page}/{size}", method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<Page&>lt;Employee> listEmployees(@RequestBody Employee findBy, 
				@PathVariable("page") int page, @PathVariable("size") int size) {
                      :
		return new ResponseEntity<Page&>lt;Employee>(employeePaginatedList, HttpStatus.OK);
	}
}

Customized swagger-ui/api docs

See the customized names and descriptions below (highlighted in yellow)