人生到头来,就是不断放下,但永远最令人心痛的就是,来不及好好|

durtime

园龄:4年8个月粉丝:10关注:1

spring boot集成swagger2的3.x版本

1、maven添加依赖

1
2
3
4
5
6
<!-- swagger -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

 2、添加配置类

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
 
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
 
@Configuration
@EnableOpenApi
public class Swagger2Config implements WebMvcConfigurer {
 
    /**
     * 是否开启swagger配置,生产环境需关闭
     */
    @Value("${swagger.enabled}")
    private boolean enable;
 
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30).pathMapping("/")
                .enable(enable)
                .apiInfo(this.apiInfo())
                .select() // 指定需要发布到Swagger的接口目录,不支持通配符
                .apis(RequestHandlerSelectors.basePackage("com.myblog.blogboot.controller"))
                .paths(PathSelectors.any())
                .build()
                // 支持的通讯协议集合
                .protocols(this.newHashSet("https", "http"));
    }
 
    /**
     * 项目信息
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("Swagger Api Doc")
                .description("SpringBoot后台接口")
                .contact(new Contact("user", null, "durtime@qq.com"))
                .version("Application Version: 1.0.0")
                .build();
    }
 
    @SafeVarargs
    private final <T> Set<T> newHashSet(T... ts) {
        if (ts.length > 0) {
            return new LinkedHashSet<>(Arrays.asList(ts));
        }
        return null;
    }
}

 

3、添加自定义配置

1
2
3
# ===== 自定义swagger配置 ===== #
swagger:
  enabled: true

 就可以基本的使用了

在使用swagger的时候,通常需要使用【@ApiParam】注解指定接口中参数的名字,特别是接口是post请求且参数使用了@RequestParam注解时

注意

3.0.0版本:需添加starter

1
2
3
4
5
<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
</dependency>

3.0.0版本以下:

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>

配置Config

  • 3.0.0之前版本需使用@EnableSwagger2注解
  • 3.0.0版本则不需要@EnableSwagger2注解,取而代之是@EnableOpenApi

ui界面地址的改变

  • 3.0.0之前的版本访问是:/swagger-ui.html
  • 3.0.0版本访问是:/swagger-ui/index.html

Docket(文档摘要信息)的文件类型配置不同

新版本配置的是 OAS_3,而老版本是 SWAGGER_2;

 

如果配置了拦截器,需要放开,添加配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Configuration
public class MvcConfig implements WebMvcConfigurer {
    @Autowired
    JWTInterceptor jwtInterceptor;
 
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/swagger**/**",
                        "/webjars/**",
                        "/v3/**",
                        "/doc.html");
    }
}

 

其他详细见官网

本文作者:durtime

本文链接:https://www.cnblogs.com/durtime/p/15972706.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   durtime  阅读(296)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
展开