pom.xml
<!-- swagger2文档生产工具-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
Swagger2类
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket creatRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.ky.springboot.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("用户管理接口API文档")
.version("1.0")
.build();
}
}
controller类
```java
@Api(tags = "用户管理模块")
@RestController
public class UserController {
@Resource
private UserService userService;
@GetMapping("user/name/{userName}")
@ApiOperation(value = "通过用户名查询用户对象")
@ApiImplicitParam(name = "userName",value = "用户名称",required = true, paramType = "path")
// @ResponseBody
public User queryUserByName(@PathVariable String userName){
return userService.queryUserByName(userName);
}
@GetMapping("user/id/{userId}")
@ApiOperation(value = "通过用户id查询用户对象")
@ApiImplicitParam(name = "userId",value = "用户id", required = true, paramType = "path")
public User queryUserById(@PathVariable Integer userId){
return userService.queryUserById(userId);
}
@PostMapping("user")
@ApiOperation(value = "添加用户")
@ApiImplicitParam(name = "user",value = "用户对象")
public Map<String,Object> addUser(User user){