swagger从2.7.0升级到3.0.0,将依赖改为3.0.0,引入springfox-boot-starter依赖,删除原来的springfox-swagger2和springfox-swagger-ui
<swagger.version>3.0.0</swagger.version><!-- <dependency>-->
<!-- <groupId>io.springfox</groupId>-->
<!-- <artifactId>springfox-swagger2</artifactId>-->
<!-- <version>${swagger.version}</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>io.springfox</groupId>-->
<!-- <artifactId>springfox-swagger-ui</artifactId>-->
<!-- <version>${swagger.version}</version>-->
<!-- </dependency>--><dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>${swagger.version}</version>
</dependency>
SwaggerConfig修改后代码如下
package io.renren.config;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.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;import java.util.ArrayList;
import java.util.List;@Configuration
public class SwaggerConfig implements WebMvcConfigurer {@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo()).select()//加了ApiOperation注解的类,才生成接口文档
// .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))//包下的类,才生成接口文档.apis(RequestHandlerSelectors.basePackage("io.renren")).paths(PathSelectors.any()).build().securitySchemes(security());}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("人人开源").description("renren-fast文档").termsOfServiceUrl("https://www.renren.io").version("3.0.0").build();}private List<SecurityScheme> security() {List<SecurityScheme> list = new ArrayList<>();list.add(new ApiKey("token", "token", "header"));return list;}
}
SwaggerConfig.java主要修改如下:
不再会引用guava依赖,删除依赖 import static com.google.common.collect.Lists.newArrayList; DocumentationType.SWAGGER_2 改成 DocumentationType.OAS_30 包下的类,才生成接口文档 .apis(RequestHandlerSelectors.basePackage("io.renren"))
修改ShiroConfig.java 配置文件
filterMap.put("/v2/api-docs", "anon"); 改成 filterMap.put("/v3/api-docs", "anon"); filterMap.put("/swagger-ui.html", "anon"); 改成 filterMap.put("/swagger-ui/index.html", "anon"); 删除 filterMap.put("/swagger/**", "anon");
最后ShiroConfig.java文件中,swagger相关配置如下:
filterMap.put("/v3/api-docs", "anon");
filterMap.put("/swagger-ui/index.html", "anon");
filterMap.put("/swagger-resources/**", "anon");
删除项目resources/static中的swagger文件夹