knife4j的最終效果:
支持直觀的入參介紹、在線調試及離線各種API文檔下載。
1 引入pom
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>3.0.2</version>
</dependency>
2 編寫配置類SwaggerConfig.java
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig implements WebMvcConfigurer{//配置Swagger2的Docket的Bean實例@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2)// apiInfo():配置 API 的一些基本信息,比如:文檔標題title,文檔描述description,文檔版本號version.apiInfo(apiInfo())// select():生成 API 文檔的選擇器,用于指定要生成哪些 API 文檔.select()// apis():指定要生成哪個包下的 API 文檔.apis(RequestHandlerSelectors.basePackage("cn.com.agree.om.controller"))// paths():指定要生成哪個 URL 匹配模式下的 API 文檔。這里使用 PathSelectors.any(),表示生成所有的 API 文檔。.paths(PathSelectors.any()).build();}//文檔信息配置private ApiInfo apiInfo() {return new ApiInfoBuilder()// 文檔標題.title("HOM項目")// 文檔描述信息.description("HOM項目在線API文檔")// 文檔版本號.version("1.0").build();}}
3 運行啟動類HomServerApplication
4 訪問swagger地址
http://localhost:8090/aom/doc.html
界面就如同文檔開始,就可以開啟完美的swagger之旅了。