Spring Boot 入門(七):集成 swagger2

本片文章是基於前一篇寫的,《Spring Boot 入門(六):集成 treetable 和 zTree 實現樹形圖》,本篇主要介紹了spring boot集成swagger2。關於swagger的介紹,自行谷歌。我這裏有在網上購買的相關視頻資料,有須要能夠呼叫我。html

1.引入相關依賴java

 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;web

 2.增長confspring

 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                 "http://www.javashuo.com/article/p-ymmsluhy-du.html","3541437581@qq.com");
43         return new ApiInfoBuilder()
44                 .title("xx項目說明文檔")
45                 .description("xx系統說明")
46                 //.termsOfServiceUrl("http://www.javashuo.com/article/p-ymmsluhy-du.html")
47                 .contact(contact)
48                 .version("1.0")
49                 .build();
50     }
51 
52 }

其中Contact構造函數中3個參數依次是:做者,地址,郵箱apache

 

 3.增長註解api

 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個註解主要對入參和出參進行增長中文註解,無關緊要。app

最後還須要在啓動類增長註解@EnableSwagger2  函數

 

 訪問:http://localhost:8080/swagger-ui.htmlpost

相關文章
相關標籤/搜索