springboot+swagger

参考:https://www.cnblogs.com/kaibindirver/p/14948036.html

目录结构

 

maven 依赖,在pom.xml 添加配置

 1 <dependency>
 2             <groupId>io.springfox</groupId>
 3             <artifactId>springfox-swagger2</artifactId>
 4             <version>2.9.2</version>
 5 </dependency>
 6  
 7 <dependency>
 8             <groupId>io.springfox</groupId>
 9             <artifactId>springfox-swagger-ui</artifactId>
10             <version>2.9.2</version>
11 </dependency>

 

 添加swagger配置文件

 1 package com.config;
 2  
 3 import org.springframework.context.annotation.Configuration;
 4 import springfox.documentation.swagger2.annotations.EnableSwagger2;
 5  
 6 //配置到springboot
 7 @Configuration
 8 //开启swagger---页面地址 /swagger-ui.html
 9 @EnableSwagger2
10 public class SwaggerConfig {
11 }

 

在配置文件添加修改:

 1 package com.config;
 2 
 3 import org.springframework.context.annotation.Bean;
 4 import org.springframework.context.annotation.Configuration;
 5 import org.springframework.core.env.Environment;
 6 import org.springframework.core.env.Profiles;
 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 import java.util.ArrayList;
15 
16 //配置到springboot
17 @Configuration
18 //开启swagger---页面地址 /swagger-ui.html
19 @EnableSwagger2
20 public class SwaggerConfig {
21 
22     //配置了Swagger 的Docket的bean实例
23     @Bean
24     public Docket docket(Environment environment){
25 //
26 ////        设置要显示的Swagger环境
27 //        Profiles profiles= Profiles.of("dev","test");
28 //        boolean flag = environment.acceptsProfiles(profiles);
29 
30         return new Docket(DocumentationType.SWAGGER_2)
31                 .apiInfo(apiInfo())
32 //                设置分组
33                 .groupName("分组-01")
34 //                //控制是否开启swagger 设置为false 浏览器就无法访问
35 //                .enable(flag)
36                 .select()
37 //        RequestHandlerSelectors配置要扫描接口的方式,下面是扫描包
38                 .apis(RequestHandlerSelectors.basePackage("com.control"))
39                 //        none()不扫描,打开swagger是空的
40 //                .apis(RequestHandlerSelectors.none())
41                 //只扫描/kai开头的接口
42 //                .paths(PathSelectors.ant("/kai/**"))
43                 .build();
44     }
45 
46 
47     //下面是再创建多一个相同的函数,实现接口分组
48     @Bean
49     public Docket docket2(Environment environment){
50 
51 ////        设置要显示的Swagger环境
52 //        Profiles profiles= Profiles.of("dev","test");
53 //        boolean flag = environment.acceptsProfiles(profiles);
54 
55         return new Docket(DocumentationType.SWAGGER_2)
56                 .apiInfo(apiInfo())
57 //                设置分组
58                 .groupName("分组-02")
59 //                .enable(flag)
60                 .select()
61                 //        RequestHandlerSelectors配置要扫描接口的方式,下面是扫描包
62                 .apis(RequestHandlerSelectors.basePackage("com.control"))
63                 .build();
64     }
65 
66     //配置Swagger信息=apiinfo
67     private ApiInfo apiInfo(){
68 //        作者信息A
69         Contact contact =new Contact("XXX",
70                 "https://i.cnblogs.com/posts/edit-done;postId=14948036",
71                 "tomcat8888@163.com");
72         return new ApiInfo(
73                 "XXXSwaggerAPi文档",
74                 "这个是我文档的描述",
75                 "v1.01",
76                 "https://i.cnblogs.com/posts/edit-done;postId=14948036",
77                 contact,
78                 "Apache 2.0",
79                 "http://www.apache.org/licenses/LICENSE-2.0",
80                 new ArrayList());
81 
82     }
83 }

 

posted @ 2022-10-14 13:20  amim  阅读(21)  评论(0编辑  收藏  举报