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
posted @ 2019-11-14 11:46  坏猫先生  阅读(1481)  评论(0编辑  收藏  举报