springboot-knife4j
1. 引入依赖
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<!--在引用时请在maven*仓库搜索最新版本号-->
<version>2.0.2</version>
</dependency>
2. Swagger配置类
/**
* @description: @EnableKnife4j开启Swagger的增强注解
* @author: Li Kang
* @create: 2020-05-05 15:50
*/
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {
@Bean(value = "defaultApi2")
public Docket defaultApi2() {
Docket docket=new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//分组名称
.groupName("默认接口")
.select()
//这里指定Controller扫描包路径
.apis(RequestHandlerSelectors.basePackage("com.lk.controller"))
.paths(PathSelectors.any())
.build();
return docket;
}
/**
* @Description: 构建 api文档的信息
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 设置页面标题
.title("使用knife4j构建的api接口文档")
// 设置作者
.contact(new Contact("李康","www.vivo.com","jonaslee214@163.com"))
// 描述
.description("欢迎访问xxx接口文档,这里是描述信息")
// 定义版本号
.version("1.0")
.build();
}
}
默认访问地址是:http://${host}:${port}/doc.html
官方文档:https://doc.xiaominfo.com/knife4j/
3. Controller
3.1 UserContller
/**
* @description: @ApiSort Controller排序用的
* @author: Li Kang
* @create: 2020-05-05 16:14
*/
@Api(value = "用户接口",tags = "用户操作相关接口")
@ApiSort(287)
@Controller
public class UserController {
@ApiOperationSupport(author = "李康")
@ApiOperation(value = "登录接口")
@GetMapping("/login")
@ResponseBody
public String login(){
return "likang";
}
@PostMapping("/register")
@ResponseBody
public String register(){
return "注册成功!";
}
}
3.2 CartContller
/**
* @description: 购物车相关接口
* @author: Li Kang
* @create: 2020-05-05 17:00
*/
@Api(value = "购物车接口",tags = "购物车操作相关接口")
@ApiSort(286)
@Controller
public class CartController {
@ApiOperationSupport(author = "李康")
@ApiOperation(value = "添加商品")
@GetMapping("/addCart")
@ResponseBody
public String addCart(){
return "添加成功";
}
}
4. application.yml
server:
port: 8080
knife4j:
# 自定义文档
markdowns: classpath:markdown/*
# production: true 生产环境开启,会屏蔽所有Swagger的相关资源
## 开启Swagger的Basic认证功能,默认是false
basic:
enable: true
username: admin
password: 123