介绍
- 开发中的接口文档很重要
- 我最开始时使用postman手写,后面用了apidoc,最后才用的swagger
- 说说swagger吧
- 功能很强大
- 自带的参数判空验证很鸡肋
- 脱离了swagger的前端页面就完全失去了作用
- 对代码的入侵特别大
- 到处都是他的注解
- 还有很多配置文件
- 为什么还要用呢
- 他们都用为了统一
- 我又开始是拒绝的,其实apidoc很不错的
- 自动化程度高
- 建立在注解的基础上的
- 文档清晰
- 建立在更换了UI界面基础上的
- 原生的像屎
- 他们都用为了统一
- 此模块满足了以下功能
- 将一些配置提取到了配置文件
- 虽然现在新的版本好像自己就做了,但是我弄的时候没有
- 使用了开源UI界面把原生的替换掉了
- 将一些配置提取到了配置文件
引入依赖
单机跟cloud的唯一区别就是 cloud可以使用 nacos聚合文档
springboot 单机版
已测试
单机版没有依赖cloud相关的组件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<!-- springboot 单机版 -->
<groupId>com.detabes</groupId>
<artifactId>doc-swagger-boot</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
springcloud版
暂时未测试
使用了nacos注册中心
<!-- springcloud doc 版 -->
<dependency>
<groupId>com.detabes</groupId>
<artifactId>doc-swagger-cloud</artifactId>
<version>1.0.0</version>
</dependency>
<!-- springcloud版 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<!-- nacos-config -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.7</version>
</dependency>
开始配置
配置下包扫描就行了(默认扫描的是 com.detabes.controller )
-
springboot 单机版
detabes.swagger.base-package=com.example.doc.swager.boot.controller
springcloud版
bootstrap.yml ```yaml server: port: 1241 spring: main: allow-bean-definition-overriding: true profiles: active: def application: name: cloud-demo detabes: swagger: base-package=com:
example:
doc:
swager:
boot:
controller: com.example.doc.swagger.cloud.controller
logging: config: classpath:logback-spring.xml debug: false
- bootstrap-def.yml
```yaml
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 # 注册中心
config:
server-addr: 127.0.0.1:8848 # 配置中心
file-extension: yaml #文件扩展名格式,针对于默认的{spring.application.name}-${profile}.${file-extension:properties}配置
enabled: true #开启或关闭配置中心
encode: UTF-8
示例测试
- 接口示例 (boot - cloud) ```java
import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
/**
- @author tn
- @version 1
- @ClassName SwaggerController
- @description 测试swaggger接口文档
@date 2020/12/22 15:12 */ @RestController @RequestMapping(“swagger”) @Api(tags = “测试swaggger接口文档”) public class SwaggerController {
@GetMapping(“/getMethod”) @ApiOperation(value = “测试接口1”, notes = “测试swaggger接口文档”) public String getMethod() {
return "hi swagger";
}
@GetMapping(“/getMethodTwo”) @ApiOperation(value = “测试接口2”, notes = “测试swaggger接口文档”) @ApiImplicitParam(value = “姓名”, name = “name”, dataTypeClass = String.class) public String getMethodTwo(String name) {
return "hi swagger:"+name;
} }
```