说明
你的项目已经使用了sawgger2.x版本。
升级
新特性
https://github.com/springfox/springfox
NOTE: Would love feedback to make this better
-
Remove explicit dependencies on springfox-swagger2
-
Remove any @EnableSwagger2… annotations
-
Add the springfox-boot-starter dependency
-
Springfox 3.x removes dependencies on guava and other 3rd party libraries (not zero dep yet! depends on spring plugin and open api libraries for annotations and models) so if you used guava predicates/functions those will need to transition to java 8 function interfaces.
-
移除了2.x版本的冲突版本,移除了guava等
-
移除了@EnableSwagger2
-
新增了springfox-boot-starter
新增了starter
删除之前的依赖,直接使用最新的starter方式。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
</parent>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
然后应用主类增加注解@EnableOpenApi,删除之前版本的SwaggerConfig.java,启动项目,访问地址:http://localhost:8088/swagger-ui/index.html,注意2.x版本中访问的地址的为http://localhost:8088/swagger-ui.html

变化还是很大的。
拦截配置
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.
addResourceHandler("/swagger-ui/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
.resourceChain(false);
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/swagger-ui/")
.setViewName("forward:/swagger-ui/index.html");
}
}
可选配置
@Configuration
public class Swagger3Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger3接口文档")
.description("文档描述")
.contact(new Contact("fanl", "#", "862844083@qq.com"))
.version("1.0")
.build();
}
}
SpringSecurity 拦截
private static final String[] AUTH_WHITELIST = {
// -- swagger ui
"/swagger-ui.html",
"/swagger-ui/*",
"/swagger-resources/**",
"/v2/api-docs",
"/v3/api-docs",
"/webjars/**"
};
//追加
.antMatchers(AUTH_WHITELIST).permitAll()
本文介绍了如何将项目中的Swagger2.x版本升级到SpringFox的Swagger-ui 3.0.0,包括新特性和升级步骤。升级过程中需要移除旧的依赖,添加springfox-boot-starter,并更新拦截配置。启动项目后,访问URL发生了变化,需要注意。
1万+

被折叠的 条评论
为什么被折叠?



