@RestController
@Api(value = "user-api", description = "User API")
@RequestMapping("/api/users")
public class UserController {
@Autowired
private UserService userService;
@ApiOperation(value = "Get user by ID", notes = "Returns a user based on ID")
@GetMapping("/{id}")
public User getUserById(@PathVariable("id") long id) {
return userService.getUserById(id);
}
@ApiOperation(value = "Create user", notes = "Creates a new user")
@PostMapping("/")
public User createUser(@RequestBody User user) {
return userService.createUser(user);
}
@ApiOperation(value = "Update user", notes = "Updates an existing user")
@PutMapping("/{id}")
public User updateUser(@PathVariable("id") long id, @RequestBody User user) {
return userService.updateUser(id, user);
}
@ApiOperation(value = "Delete user", notes = "Deletes an existing user")
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable("id") long id) {
userService.deleteUser(id);
}
}
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket apiDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.api"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API documentation")
.description("API documentation using Swagger")
.version("1.0")
.build();
}
}