Spring Boot集成Swagger导入YApi@无界编程
接口APi開發現狀
現在開發接口都要在類似YApi上寫文檔,這樣方便不同的團隊之間協作,同步更新接口,提高效率。
但是如果接口很多,你一個個手工在YApi去錄入無疑效率很低。
如果是使用Spring Boot集成Swagger可以直接導入YApi非常方便,不過還有一些需要注意的事項。
?
1.Spring Boot集成Swagger
添加swagger相關的maven依賴
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version>
</dependency>
?
2.添加swagger的配置類
@Configuration
@EnableSwagger2
@ComponentScan(basePackages = { "xxx.controller" })//掃描的包路徑
public class SwaggerConfig {@Beanpublic Docket api() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().paths(PathSelectors.any()).build();}/*** 該套 API 說明,包含作者、簡介、版本、host、服務URL* @return*/private ApiInfo apiInfo() {return new ApiInfoBuilder().title("api 說明")//接口標題.description("商品列表接口")//接口描述.version("v1.0")//版本號.contact(new Contact("name", "url", "email"))//聯系人信息.build();}
}
或者
@Configuration
@EnableSwagger2
public class SwaggerConfig {@Beanpublic Docket api() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("xxx.controller")) //掃描的包路徑.build();}/*** 該套 API 說明,包含作者、簡介、版本、host、服務URL* @return*/private ApiInfo apiInfo() {return new ApiInfoBuilder().title("api 說明").contact(new Contact("allen","null","name@example.com")).version("0.1").termsOfServiceUrl("localhost:8080/demo/").description("demo api").build();}}
?
3.使用Swagger注解
@Api()用于類;
標識這個類是swagger的資源
tags–表示分組說明標簽
@ApiOperation()用于方法;
表示一個http請求的操作
value用于方法描述?
notes用于提示內容
@ApiModel()用于實體類
表示對類進行說明,用于參數用實體類接收
?? ?? value–表示對象名?
?? ?? description–描述
@ApiModelProperty()用于實體類字段
表示對model屬性的說明或者數據操作更改
value–字段說明?
name–重寫屬性名字?
dataType–重寫屬性類型?
required–是否必填?
example–舉例說明?
hidden–隱藏
@ApiImplicitParam() 用于 controller 方法
表示單獨的請求參數
name–參數ming
value–參數說明
dataType–數據類型
paramType–參數類型
example–舉例說明
@ApiImplicitParams() 用于 controller 方法,包含多個 @ApiImplicitParam
@ApiIgnore()用于類或者方法上,可以不被swagger顯示在頁面上
說明:簡單的標記只需要@Api(tags="") 和 @ApiOperation(value="",notes="")
更多參考:https://github.com/swagger-api/swagger-core/wiki/Annotations
?
4.Java代碼寫上Swagger注解
@Api(tags = "xxx查詢列表Api")
@RestController
@RequestMapping(value = {"/xx/"}, produces = {MediaType.APPLICATION_JSON_VALUE})
public class xxxApiController {@GetMapping("/xxx/getAllList")*/@ApiOperation("xxxx-查詢列表")@PostMapping(value = {"/xxx/getList", "/front/getCount"}
5.查看Swagger UI
隨著你系統的URL路徑不同而不同,默認在這
http://localhost:8080/swagger-ui.html??
如果你有服務的前綴xxx-service加上即可?
http://localhost:8080/xxx-service/swagger-ui.html??
查看某一個controller下的接口列表:
查看某個具體接口:
?返回值
至此swagger的任務已經完成。
?
6.swagger導入YApi
swagger ui顯然看起來還是不方便,目前很多公司都在用YApi做接口的標準文檔管理了。
YAPI里點 數據管理? ?,然后導入swagger的json數據即可。
注意這里YAPI號稱支持導入swagger的URL,發現不好用,導入不進來。
?
回到swagger的UI界面
點這個鏈接,打開復制數據存為xxx.json即可,如果存txt也許會亂碼
導入yapi即可
?在Ypai里看起來是不是清爽很多:
總結
以上是生活随笔為你收集整理的Spring Boot集成Swagger导入YApi@无界编程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 黄山风景区一天能游玩完吗
- 下一篇: 觉醒仙力