文章目录
高版本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并不能跑起来
其原因为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)
此注解与上述的全局配置的作用相同,只不过只能一个类写一个注解
使用之后的效果: