Write and flow in life.|

逐东

园龄:3年3个月粉丝:1关注:0

问题记录分析:配置swagger时找不到配置类的问题

在项目中引入swagger,用于测试接口。

首先建立了另外一个与子工程模块(service模块)同级的模块common,放一些需要的配置,在这个模块中建立子模块service_base,在里面配置swagger。

建立com.yi.eduservicebase.config包,创建SwaggerConfig类

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket webApiConfig(){

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();

    }

    private ApiInfo webApiInfo(){

        return new ApiInfoBuilder()
                .title("网站-课程中心API文档")
                .description("本文档描述了课程中心微服务接口定义")
                .version("1.0")
                .contact(new Contact("yi", "http://test.com", "yi@qq.com"))
                .build();
    }
}

然后在service模块的maven中注册

<!--        在serviceedu中引入service依赖,然后才能在edu中使用-->
        <dependency>
            <groupId>com.yi</groupId>
            <artifactId>service_base</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

最后在service模块的spingboot启动类中加入@ComponentScan注解,扫描swagger配置类所在的包

//springboot启动类
@SpringBootApplication
//必须要在启动类中加此配置,才会加载到配置类中内容。
//因为此配置类写在了其他模块
@ComponentScan(basePackages = {"com.yi"})
public class Eduapplication {
    public static void main(String[] args) {
        SpringApplication.run(Eduapplication.class, args);
    }
}

随后启动项目,发现swagger没有启动成功,反而弹出一个窗口

在这里插入图片描述

原因是swagger配置类没有找到。但是明明按照正常方式注册了,为什么没有找到?

去网上搜解决方法也没有解决问题。

最终解决问题,发现原来是maven缓存的问题,点一下pom文件(将外面的common/servicebase模块注册进service模块)旁边的maven刷新小按钮。问题解决。

本文作者:逐东

本文链接:https://www.cnblogs.com/vuds/p/15986617.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   逐东  阅读(673)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起