一.Swagger簡介
Swagger是一組圍繞OpenAPI規(guī)范構(gòu)建的開源工具,可以幫助你設(shè)計(jì)、構(gòu)建、記錄和使用REST API。主要的Swagger工具包括:
Swagger Editor:基于瀏覽器的編輯器,你可以在這里編寫OpenAPI規(guī)范。
Swagger UI:將OpenAPI規(guī)范渲染成交互式的API文檔。
Swagger Codegen:從OpenAPI規(guī)范中生成服務(wù)器存根和客戶端庫。
二.SSM整合Swagger
1.引入Maven
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency>
2.添加SwaggerConfig
package cn.atwangjian.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * @author wangjian * @date 2020/5/25 20:01 */ @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket userApi() { return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); } @Bean public RequestMappingInfoHandlerMapping requestMapping() { return new RequestMappingHandlerMapping(); } }
3.springMvc.xml添加Swagger配置
<!--添加swagger配置--> <!-- 引入swagger相關(guān) --> <bean class="cn.atwangjian.config.SwaggerConfig"/> <!--使用swagger --> <!--<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />--> <!--使用knife4j --> <mvc:resources mapping="doc.html" location="classpath:/META-INF/resources/" /> <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" /> <bean class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration" id="swagger2Config"/>
三.Swagger效果
訪問:http://IP:port/web-obj-name/swagger-ui.html
四.Swagger常用屬性
@Api() 用于類;表示標(biāo)識(shí)這個(gè)類是swagger的資源 tags–表示說明 value–也是說明,可以使用tags替代 @ApiOperation() 用于方法;表示一個(gè)http請(qǐng)求的操作 value用于方法描述 notes用于提示內(nèi)容 tags可以重新分組 @ApiParam() 用于方法,參數(shù),字段說明;表示對(duì)參數(shù)的添加元數(shù)據(jù)(說明或是否必填等) name–參數(shù)名 value–參數(shù)說明 required–是否必填 @ApiModel() 用于類 ;表示對(duì)類進(jìn)行說明,用于參數(shù)用實(shí)體類接收 value–表示對(duì)象名 description–描述 都可省略 @ApiModelProperty() 用于方法,字段; 表示對(duì)model屬性的說明或者數(shù)據(jù)操作更改 value–字段說明 name–重寫屬性名字 dataType–重寫屬性類型 required–是否必填 example–舉例說明 hidden–隱藏
五.Knife4j簡介
knife4j是為集成Swagger生成Api文檔的增強(qiáng)解決方案
六.Knife4j核心功能
文檔說明
knife4j的列出接口文檔的說明,包括接口地址、請(qǐng)求方式、接口描述、請(qǐng)求類型、請(qǐng)求示例、請(qǐng)求參數(shù)、響應(yīng)狀態(tài)、響應(yīng)示例等。在線調(diào)試
knife4j提供了強(qiáng)大的在線接口調(diào)試功能,自動(dòng)解析當(dāng)前接口參數(shù),同時(shí)包含表單驗(yàn)證,調(diào)用參數(shù)可返回接口響應(yīng)內(nèi)容、Raw、Headers、Curl以及響應(yīng)耗時(shí)等信息。幫助開發(fā)者在線調(diào)試,而不用通過其他三方工具測(cè)試接口是否正確。
七.SSM整合Knife4j
maven中引入jar包
正如knife4j官網(wǎng)介紹所說,如果你是一名Java開發(fā)工程師,那么使用swagger-bootstrap-ui將會(huì)非常簡單,只需要在原使用的基礎(chǔ)上,添加swagger-bootstrap-ui的maven引用jar包即可。
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.6</version> </dependency>
2.添加SwaggerConfig配置和springMvc配置
同swagger篇
八.Knife4j效果
訪問地址:http://IP:port/web-obj-name/doc.html
1.文檔
knife4j為我們寫好了接口文檔,可以直接復(fù)制導(dǎo)出,非常的方便。
2.調(diào)試
調(diào)試成功:
調(diào)試報(bào)錯(cuò)都會(huì)顯示: