一.环境搭建
1.依赖
<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>
<!-- 高版本SpringBoot 与swagger可能出现版本冲突-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
2.配置Swagger配置类
@Configuration
//Swagger2支持(复合注解)
@EnableSwagger2
public class SwaggerConfig {
//进行分组
@Bean
public Docket docket1(){
return new Docket(DocumentationType.SWAGGER_2).groupName("第一组");
}
@Bean
public Docket docket2(){
return new Docket(DocumentationType.SWAGGER_2).groupName("第二组");
}
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).
// RequestHandlerSelectors.basePackage 表明选择扫描那个包
select().apis(RequestHandlerSelectors.basePackage("top.remained.controller"))
.build();
}
private ApiInfo apiInfo(){
//配置个人信息 url为访问swagger的地址
Contact contact=new Contact("lx","http://localhost:8080/swagger-ui.html","123456@qq.com");
return new ApiInfo(
"lx的SwaggerAPI文档",//标题
"Api Documentation",//描述
"1.0", //版本
"urn:tos",//邮箱
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
二使用
1.测试
1.页面
2.所分的组
2.关于bean的注解
@ApiModel("用户")
public class User {
@ApiModelProperty("标识")
private int id;
@ApiModelProperty("名字")
private String name;
@ApiModelProperty("类型")
private int type;
@ApiModelProperty("密码")
private String pwd;
}
效果
3.关于Controller注解
@Api("user")
public class UserController {
@Autowired
UserService userService;
@ApiOperation("登录")
@GetMapping("/login/{name}/{pwd}")
public User login(
@ApiParam("用户名") @PathVariable("name") String name,
@ApiParam("密码") @PathVariable("pwd") String pwd){
return userService.login(name, pwd);
}
@ApiOperation("所有用户")
@GetMapping("/getAllUsers/{index}")
public List<User> getAllUsers(
@ApiParam("页数") @PathVariable("index") Integer index){
if (index==null){
index=0;
}
return userService.findAllUsers(index);
}
@ApiOperation("删除用户")
@GetMapping("/delete/{id}")
public int delUser(
@ApiParam("删除的用户Id") @PathVariable("id") Integer id){
return userService.delUser(id);
}
@ApiOperation("修改用户")
@GetMapping("/update")
public int updateUser(@ApiParam("更新的用户") User user){
return userService.updateUser(user);
}
@ApiOperation("添加用户")
@GetMapping("/add")
public int addUser(@ApiParam("添加的用户") User user){
return userService.insertUser(user);
}
}
4.效果