首页 > 其他分享 >springboot+swagger

springboot+swagger

时间:2022-10-14 13:35:19浏览次数:82  
标签:swagger springboot documentation org import springfox com

参考: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                 "[email protected]");
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 }

 

标签:swagger,springboot,documentation,org,import,springfox,com
From: https://www.cnblogs.com/amim/p/16791322.html

相关文章

  • SpringBoot(三) - Slf4j+logback 日志,异步请求,定时任务
    1、Slf4j+logback日志SpringBoot框架的默认日志实现:slf4j+logback;默认日志级别:info,对应了实际生产环境日志级别;1.1日志级别#常见的日志框架中,日志级别都包含五种,......
  • spring boot使用swagger生成api接口文档
    前言在之前的文章中,使用mybatis-plus生成了对应的包,在此基础上,我们针对项目的api接口,添加swagger配置和注解,生成swagger接口文档具体可以查看本站springboot系列文章:s......
  • SpringBoot 2.x 集成kaptcha生成图形验证码
    Kaptcha框架介绍谷歌开源的一个可高度配置的实用验证码生成工具验证码的字体/大小/颜色验证码内容的范围(数字,字母,中文汉字!)验证码图片的大小,边框,边框粗细,边框颜色......
  • 初识SpringBoot
    什么是SpringSpring是一个开源框架,2003年兴起的轻量级的Java开发框架。作者:RodJohnsonSpring是为解决企业级应用开发的复杂性而创建的,简化开发。Spring如何简化Java......
  • springboot项目中swagger配置
    1、导入依赖查看代码<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>3.0.2</vers......
  • springboot/spring使用ConfigurationProperties注解读取自定义属性(尚硅谷)
    pom.xml<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xs......
  • SpringBoot集成JMH进行基准测试
    基准测试时评估程序/接口性能的一个有效的方法。笔者早期在网上了解到​​JMH​​这个代码工具。由于之前找到的一些代码不具备项目实践场景的说服力,通常是一项简单算法,独立......
  • 几行代码,搞定 SpringBoot 接口恶意刷新和暴力请求!
    在实际项目使用中,必须要考虑服务的安全性,当服务部署到互联网以后,就要考虑服务被恶意请求和暴力攻.击的情况,下面的教程,通过​​intercept​​和​​redis​​针对​​url+ip......
  • 教你优雅的实现 SpringBoot 并行任务
    SpringBoot的定时任务:第一种:把参数配置到.properties文件中:代码:packagecom.accord.task;importjava.text.SimpleDateFormat;importjava.util.Date;importorg.spring......
  • SpringBoot深入理解
    当使用打包时,会下载org-springframework-boot-loader的jar,并且不会放在lib存放的第三方jar包文件中,该jar包中有个JarLauncher.class文件中设置了jar包运行时的入口和打包后......