ruoyi系统默认集成的swagger 显示有点太丑了集成Knife4j 美观升级功能也更加的实用

1 增加引入Knife4j

目前最新版版是2.0.9
打开ruoyi项目 common模块 pom.xml

        <!-- https://mvnrepository.com/artifact/io.swagger.core.v3/swagger-annotations
            如果开发者使用OpenAPI3的结构,底层框架依赖springfox3.0.0,可以考虑Knife4j的3.x版本
        -->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <!--在引用时请在maven中央仓库搜索3.X最新版本号-->
            <version>2.0.9</version>
        </dependency>

2 修改配置 SwaggerConfig.java

1增加注解

@Configuration
@EnableSwagger2WebMvc (新增注解)
public class SwaggerConfig

2解决报错

    @Bean
    public Docket createRestApi()
    {
        return new Docket(DocumentationType.SWAGGER_2) 

3解决报错

    private List<SecurityContext> securityContexts()
    {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.any())
                        //.operationSelector(o -> o.requestMappingPattern().matches("/.*"))
                        .build());
        return securityContexts;
    }

3删除无用的引用

1 删除根目录pom.xml swagger引用

            <!-- Swagger3依赖 -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-boot-starter</artifactId>
                <version>${swagger.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>io.swagger</groupId>
                        <artifactId>swagger-models</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

2 删除admin模块pom.xml swagger引用

	<swagger.version>3.0.0</swagger.version>
        <!-- swagger3-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
        </dependency>

        <!-- 防止进入swagger页面报类型转换错误,排除3.0.0中的引用,手动增加1.6.2版本 -->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.6.2</version>
        </dependency>

Logo

快速构建 Web 应用程序

更多推荐