Spring boot整合Swagger功能

一、引入包,pom.XML配置如下

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

 

 

二、编写配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.anjiplus.template.gaea.business.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
/**
 * @Author 尹明能
 * @Date 2022/4/12 12:52
 * @Version 1.0
 */
@EnableSwagger2
@Configuration
public class Swagger2Config {
 
    @Bean
    public Docket createRestApi() {
         return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.anjiplus.template.gaea.business.modules"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("利用Swagger2构建RESTful APIs")
                .description("swagger demo")
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }
}

  

三、拦截规则配置,否则会出现swagger打开白屏问题

 

这个是我自己写的规则,如果使用spring securiry参考其他方式

1
kip-authenticate-urls: /gaeaDict/all, /login, /static, /file/download/, /index.html,/swagger,/webjars,/v2,/swagger-resources,/swagger-ui.html, /favicon.ico, /reportShare/detailByCode,/reportDashboard

  

四、控制器增加@API注解

posted on   topguntopgun  阅读(37)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示