1.Swagger簡介
說白了就是實時更新api接口,以免前后端工程師打架
2.新建一個springboot(Web)項目
2.1導入maven依賴
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- 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>
2.2新建一個conreoller
package com.yao.swagger.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String hello(){ return "hello"; } }
2.2.1啟動SpringbootSwaggerApplication主入口進入檢查
(擴展:一個項目只寫了一個controller那么這個項目有幾個請求,答案為兩個,因為無論是否有controller都至少有一個請求那就是error,就是那個報錯頁面)
2.3配置swagger基本信息
2.3.1創建一個config
package com.yao.swagger.config; import org.springframework.context.annotation.Configuration; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { }//以上含有的注解,是最簡單一定要加的至少兩個基本注解
2.3.2訪問http://localhost:8080/swagger-ui.html測試,會發現直接就有了這個頁面,我們明明都沒有創建這個頁面
又由於swagger有一個bean對象在springboot中
我們接着寫它的配置類:
package com.yao.swagger.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; import static springfox.documentation.service.ApiInfo.DEFAULT_CONTACT; @Configuration @EnableSwagger2 public class SwaggerConfig { //配置了Swapperbean實例 @Bean public Docket docket(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()); } //配置Swagger信息,就是一些默認的配置信息 private ApiInfo apiInfo(){ //作者信息 Contact contact = new Contact( "幺幺", "https://www.cnblogs.com/yaoyaoo/", "892095368@qq.com"); return new ApiInfo( "幺幺的SwaggerApi", "這個作者很強", "1.0", "https://www.cnblogs.com/yaoyaoo/", contact, //以下三個配置默認 "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList()); } }
2.4配置掃描接口及開關
package com.yao.swagger.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.core.env.Profiles; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; import java.util.Properties; import static springfox.documentation.service.ApiInfo.DEFAULT_CONTACT; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket docket1(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("A"); } @Bean public Docket docket2(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("B"); } @Bean public Docket docket3(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("C"); } //配置了Swapperbean實例 @Bean public Docket docket(Environment environment){ // 設置要顯示的swagger環境 Profiles profiles = Profiles.of("dev","test"); // 通過環境監聽查看自己是否處在設置的環境之中 boolean flag = environment.acceptsProfiles(profiles); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .groupName("幺幺") //enable如果為false,則swagger就不能在瀏覽器中訪問 //enable的應用就主要在於,生產環境中使用swagger,發布時就不使用swagger,那么其實我們只用 // 去改變這個enable的值就可以了 .enable(flag) .select() //select和build是一套,apis和paths只能在select和build之間來配置 //RequestHandlerSelectors 配置要掃描接口的方式 //basePackage指定該掃描的包 //RequestHandlerSelectors.any()掃描所有包 //RequestHandlerSelectors.none()都不掃描 //RequestHandlerSelectors.withClassAnnotation()掃描類上的注解需要傳入一個反射出來的類 //比如說傳入一個xxx。class //RequestHandlerSelectors.withMethodAnnotation()掃描方法上的注解 //因為有一些注解是用在類上的,有一些是放在方法上的比如@Controller這個注解就只能放在類上因 // 此它只能放在RequestHandlerSelectors.withClassAnnotation()中 //RequestHandlerSelectors.withClassAnnotation(RestController.class)就只會掃描有 // RestController注解的類 .apis(RequestHandlerSelectors.basePackage("com.yao.swagger.controller")) //paths代表可以過濾什么路徑,只掃描帶有yao下面請求的接口,打個比方RequestMapping("/hello") // 就不會被掃描,但是RequestMapping("/yao/hello")就可以被掃描 //PathSelectors.any()就是什么都會掃描到,none()等等以此類推 .paths(PathSelectors.ant("yao/**")) .build(); } //配置Swagger信息,就是一些默認的配置信息 private ApiInfo apiInfo(){ //作者信息 Contact contact = new Contact( "幺幺", "https://www.cnblogs.com/yaoyaoo/", "892095368@qq.com"); return new ApiInfo( "幺幺的SwaggerApi", "這個作者很強", "1.0", "https://www.cnblogs.com/yaoyaoo/", contact, //以下三個配置默認 "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList()); } }
主要就是在docket里面去配置
2.4.1在類上配置注解
package com.yao.swagger.pojo; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @ApiModel("用戶實體類") public class User { @ApiModelProperty("用戶名") public String username; @ApiModelProperty("密碼") public String password; }
除了在實體類上還可以在controller中
實時測試