高版本兼容swagger 一系列问题

高版本SpringBoot集成Swagger2.0的一系列问题

配置Swagger
引入依赖:
 <!--        配置Swagger-ui-->
        <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>
            <!--            排除自带的1.5.20版本-->
            <exclusions>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-models</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--        使用1.5.22版本-->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.5.22</version>
        </dependency>

或者直接使用内置的

 <!--        配置Swagger-ui-->
        <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>
配置swagger的配置类swaggerconf
package com.example.security.conf;

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.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@EnableSwagger2
@Configuration
public class SwaggerConf {
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .useDefaultResponseMessages(false)
                .groupName("所有人")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.security"))
                .paths(PathSelectors.any())
                .build()
            	//引入security进行验证
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("测试")
                .description("just a test")
                .version("1.0")
                .contact(new Contact("wxk", "http://localhost:8080/swagger-ui.html", ""))
                .build();
    }

    private List<ApiKey> securitySchemes() {
        List<ApiKey> apiKeys = new ArrayList<>();
        apiKeys.add(new ApiKey("token", "token", "header"));
        return apiKeys;
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build());
        return securityContexts;
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference("token", authorizationScopes));
        return securityReferences;
    }
}
测试:

当我们配置玩swagger之后,发现springboot并不能跑起来

wxk

其原因为SpringBoot在2.6.x时采用的匹配模式不一样

springfox假设springmvc的路径匹配策略为ant-path-matcher,而springboot2.6.x的默认匹配策略为path-pattern-matcher

这是其报错的根本原因,那么如何解决?

解决高版本spring boot集成swagger 2.0 报错
方法一: 直接在yaml文件中进行设置,修改其匹配模式
spring: 
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

此方法简单明了但是可能会产生其他的影响,但是目前还没有遇见

方法二: 创建一个类实现 WebMvcConfigurationSupport
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    /**
     * 发现如果继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。 需要重新指定静态资源
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html", "doc.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }
}

这种方法代码长,只是修改了swagger部分,相对可靠,但是存在一些问题,会使Jackson失效

在项目中我们一半使用全局返回方法,这个方法有时候可能存在这null值,但是我们不想让null值返回回去,我们可以使其不进行一个序列化,这时候使用Jackson进行操作,使其不进行一个序列化

spring:
  jackson:
    default-property-inclusion: non_null

但是如果我们试了WebMvcConfigurationSupport之后,发现Jackson就已经失效了:

在这里插入图片描述

yml配置文件中的东西无法进行一个全局配置,但是还存在这一些其他的方法那就是在方法上添加一个注解

@JsonInclude(JsonInclude.Include.NON_NULL)

此注解与上述的全局配置的作用相同,只不过只能一个类写一个注解

使用之后的效果:

在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值