一、簡介
swagger是一款流行的API框架,用於生成服務文檔,可視化、可調用、自定義規範,簡化了開發過程,實時同步API文檔的強大功能,相對於其餘手寫API文有極大優點html
二、使用
-
依賴 版本不一樣,自行選擇spring
<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>
-
配置 1api
@Configuration @EnableSwagger2 public class Swagger { [@Bean](https://my.oschina.net/bean) public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .groupName("分組") .select() .apis(RequestHandlerSelectors.basePackage("com.test.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring boot Swagger-ui") .description("描述信息") .termsOfServiceUrl("http://www.baidu.com/") .version("1.0") .build(); } }
-
配置 2app
@EnableSwagger2 @Configuration public class Swagger { @Bean public Docket swaggerApi() { return new Docket(DocumentationType.SWAGGER_2) .groupName("/hello") .genericModelSubstitutes(DeferredResult.class) .useDefaultResponseMessages(false) .forCodeGeneration(true) .pathMapping("")// api測試請求地址,最終調用接口後會和paths拼接在一塊兒 .select() .paths(PathSelectors.regex("/.*"))// 過濾的接口 .build(); } }