本片文章是基於前一篇寫的,《Spring Boot 入門(六):集成 treetable 和 zTree 實現樹形圖》,本篇主要介紹了spring boot集成swagger2。關於swagger的介紹,自行谷歌。我這裡有在網上購買的相關視頻資料,有需要可以呼叫我。 1.引入相關依賴 很多地方只引入 ...
本片文章是基於前一篇寫的,《Spring Boot 入門(六):集成 treetable 和 zTree 實現樹形圖》,本篇主要介紹了spring boot集成swagger2。關於swagger的介紹,自行谷歌。我這裡有在網上購買的相關視頻資料,有需要可以呼叫我。
1.引入相關依賴
1 <dependency> 2 <groupId>io.springfox</groupId> 3 <artifactId>springfox-swagger2</artifactId> 4 <version>2.4.0</version> 5 </dependency> 6 <dependency> 7 <groupId>io.springfox</groupId> 8 <artifactId>springfox-swagger-ui</artifactId> 9 <version>2.4.0</version> 10 </dependency> 11 12 <dependency> 13 <groupId>org.apache.directory.studio</groupId> 14 <artifactId>org.apache.commons.codec</artifactId> 15 <version>1.8</version> 16 </dependency>
很多地方只引入了前2個依賴,這裡如果缺少第3個依賴,容易產生一個異常: java.lang.NoSuchMethodError: com.google.common.collect.FluentIterable.toList()Lcom/google/common/collect/ImmutableList;
2.增加conf
3 import io.swagger.annotations.ApiOperation; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.Configuration; 6 import springfox.documentation.builders.ApiInfoBuilder; 7 import springfox.documentation.builders.PathSelectors; 8 import springfox.documentation.builders.RequestHandlerSelectors; 9 import springfox.documentation.service.ApiInfo; 10 import springfox.documentation.service.Contact; 11 import springfox.documentation.spi.DocumentationType; 12 import springfox.documentation.spring.web.plugins.Docket; 13 import springfox.documentation.swagger2.annotations.EnableSwagger2; 14 15 /** 16 * @program: 17 * @description: Swagger配置 18 * @author: DZ 19 * @create: 2019-10-14 18:41 20 **/ 21 @Configuration 22 @EnableSwagger2 23 public class SwaggerConfig { 24 25 @Bean 26 public Docket createRestApi() { 27 return new Docket(DocumentationType.SWAGGER_2) 28 .apiInfo(apiInfo()) 29 .select() 30 // 設置basePackage會將包下的所有類的所有方法作為api 31 // .apis(RequestHandlerSelectors.basePackage("com.example.demo2.controller")) 32 //只有標記了@ApiOperation的方法才會暴露出給swagger 33 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) 34 .paths(PathSelectors.any()) 35 .build(); 36 } 37 38 39 private ApiInfo apiInfo() { 40 //swagger2中termsOfServiceUrl方法已經啟用,且contact的參數時一個對象,棄用字元串了 41 Contact contact=new Contact("dz", 42 "https://www.cnblogs.com/dz-boss/p/11729334.html","[email protected]"); 43 return new ApiInfoBuilder() 44 .title("xx項目說明文檔") 45 .description("xx系統說明") 46 //.termsOfServiceUrl("https://www.cnblogs.com/dz-boss/p/11729334.html") 47 .contact(contact) 48 .version("1.0") 49 .build(); 50 } 51 52 }
其中Contact構造函數中3個參數依次是:作者,地址,郵箱
3.增加註解
6 import io.swagger.annotations.*; 7 import lombok.extern.slf4j.Slf4j; 8 import org.springframework.amqp.rabbit.connection.CorrelationData; 9 import org.springframework.amqp.rabbit.core.RabbitTemplate; 10 import org.springframework.beans.factory.annotation.Autowired; 11 import org.springframework.beans.factory.annotation.Value; 12 import org.springframework.stereotype.Controller; 13 import org.springframework.web.bind.annotation.*; 14 15 /** 16 * @program: 17 * @description: 18 * @author: DZ 19 * @create: 2019-10-09 15:27 20 **/ 21 @Api(value = "API介面", tags = "Test", description = "API介面") 22 @Slf4j25
@Controller 26 public class Test { 27 28 //測試介面 29 @ApiOperation(value = "測試", notes = "測試介面") 30 @ApiImplicitParams({ 31 @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "query", defaultValue = "123"), 32 @ApiImplicitParam(name = "userId", value = "用戶id", required = false, dataType = "String", paramType = "query", defaultValue = "654") 33 }) 34 @ApiResponses(value = { 35 @ApiResponse(code = 200, message = "Successful — 請求已完成"), 36 @ApiResponse(code = 400, message = "請求中有語法問題,或不能滿足請求"), 37 @ApiResponse(code = 401, message = "未授權客戶機訪問數據"), 38 @ApiResponse(code = 404, message = "伺服器找不到給定的資源;文檔不存在"), 39 @ApiResponse(code = 500, message = "伺服器不能完成請求")} 40 ) 41 @ResponseBody 42 @RequestMapping(value = "index", method = RequestMethod.POST) 43 public String test(@RequestParam("id") String id, @RequestParam(value = "userId", required = false) String userId) { 44 return ""; 45 } 46 }
其中關鍵的註解為類的註解:@Api和方法的註解@ApiOperation,增加了這2個註解,就可以通過swagger的方式訪問;
@ApiImplicitParams和@ApiResponses這2個註解主要對入參和出參進行增加中文註解,可有可無。
最後還需要在啟動類增加註解@EnableSwagger2
訪問:http://localhost:8080/swagger-ui.html