springboot 2.1.6.RELEASE整合Swagger2


一、引入依賴

 1    <modelVersion>4.0.0</modelVersion>
 2     <groupId>com.badcat</groupId>
 3     <artifactId>demo</artifactId>
 4     <version>0.0.1-SNAPSHOT</version>
 5     <packaging>jar</packaging>
 6     
 7     <properties>
 8         <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
 9     </properties>
10     
11     <parent>
12         <groupId>org.springframework.boot</groupId>
13         <artifactId>spring-boot-starter-parent</artifactId>
14         <version>2.1.6.RELEASE</version>
15         <relativePath/>
16     </parent>
17     
18     <dependencies>
19         <dependency>
20             <groupId>org.springframework.boot</groupId>
21             <artifactId>spring-boot-starter-web</artifactId>
22         </dependency>
23         
24         <!-- swagger2 配置 -->
25         <dependency>
26             <groupId>io.springfox</groupId>
27             <artifactId>springfox-swagger2</artifactId>
28             <version>2.4.0</version>
29         </dependency>
30         <dependency>
31             <groupId>io.springfox</groupId>
32             <artifactId>springfox-swagger-ui</artifactId>
33             <version>2.4.0</version>
34         </dependency>
35         <dependency>
36             <groupId>com.github.xiaoymin</groupId>
37             <artifactId>swagger-bootstrap-ui</artifactId>
38             <version>1.6</version>
39         </dependency>
40     </dependencies>

二、創建配置類

 

 1 package com.badcat.config;
 2 
 3 import org.springframework.context.annotation.Bean;
 4 import org.springframework.context.annotation.Configuration;
 5 import springfox.documentation.builders.ApiInfoBuilder;
 6 import springfox.documentation.builders.PathSelectors;
 7 import springfox.documentation.builders.RequestHandlerSelectors;
 8 import springfox.documentation.service.ApiInfo;
 9 import springfox.documentation.service.Contact;
10 import springfox.documentation.spi.DocumentationType;
11 import springfox.documentation.spring.web.plugins.Docket;
12 import springfox.documentation.swagger2.annotations.EnableSwagger2;
13 
14 @Configuration
15 @EnableSwagger2
16 public class Swagger2 {
17 
18 //    http://localhost:8080/swagger-ui.html     
19 //    http://localhost:8080/doc.html     
20 
21     // 配置swagger2核心配置 docket
22     @Bean
23     public Docket createRestApi() {
24         return new Docket(DocumentationType.SWAGGER_2)          // 指定api類型為swagger2
25                     .apiInfo(apiInfo())                         // 用於定義api文檔匯總信息
26                     .select()
27                     .apis(RequestHandlerSelectors
28                             .basePackage("com.badcat.controller"))   // 指定controller包
29                     .paths(PathSelectors.any())                 // 所有controller
30                     .build();
31     }
32 
33     private ApiInfo apiInfo() {
34         return new ApiInfoBuilder()
35                 .title("后台接口api")                        // 文檔頁標題
36                 .contact(new Contact("badcat",
37                         "https://www.xxxx.com",
38                         "xxxx@xxxx.com"))                     // 聯系人信息
39                 .description("后台api文檔")                  // 詳細信息
40                 .version("1.0.1")                       // 文檔版本號
41                 .termsOfServiceUrl("https://www.xxxx.com")       // 網站地址
42                 .build();
43     }
44 
45 }

 

三、啟動項目

輸入網址,可以顯示出所有接口信息,可以直接調試。下邊是兩種風格的

    http://localhost:8080/swagger-ui.html, http://localhost:8080/doc.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM