栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

高版本springboot整合swagger

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

高版本springboot整合swagger

高版本springboot整合swagger
    • 这是我的springboot版本2.6: ![![Alt](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9hdmF0YXIu](https://img-blog.csdnimg.cn/f3b9f7456e84432c9b339e69f9a32b9f.png)
    • 导入maven依赖(如果是高版本的别随便整合依赖会有问题):
    • 然后是swagger配置类
    • 然后在应用swagger模块的配置类中加上,这个一定得加,不然启动不了
    • 在你应用swagger的模块主启动一定要加上@ComponentScan("com.atguigu"),不然打开swagger什么都没有

第一次使用 高版本 springboot整合swagger存在一些问题,所以记录一下

这是我的springboot版本2.6: 导入maven依赖(如果是高版本的别随便整合依赖会有问题):
		
            io.springfox
            springfox-boot-starter
            3.0.0
        
        
            io.springfox
            springfox-swagger-ui
            3.0.0
        
然后是swagger配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    
    @Bean
    public Docket docket(){
        return  new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(true)              //是否激活swagger,如果是正式环境下,请把他设置为false
                .groupName("LittleFox")          //分组,可以设置多个docket
                .select()
                .paths(PathSelectors.any())
                .build();
    }


    public ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("LittleFox")
                .description("谷粒商城swagger")
                .version("1.0")
                .termsOfServiceUrl("termsOfServiceUrl")
                .contact(new Contact("name","url","email"))
                .build();
    }
//    下面的是默认配置 apiInfo.default
//    static {
//        DEFAULT = new ApiInfo("Api Documentation", "Api Documentation", "1.0", "urn:tos", DEFAULT_CONTACT, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
//    }
}
然后在应用swagger模块的配置类中加上,这个一定得加,不然启动不了
spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
在你应用swagger的模块主启动一定要加上@ComponentScan(“com.atguigu”),不然打开swagger什么都没有
@SpringBootApplication
@MapperScan("com.atguigu.gulimall.product.dao")
@ComponentScan("com.atguigu")	//给swagger指定地点
@EnableDiscoveryClient

public class GulimallProductApplication {
    public static void main(String[] args) {
        SpringApplication.run(GulimallProductApplication.class,args);
    }
}

最后访问地址 http://localhost:10001/swagger-ui/ (端口号改成你自己的,其他不要变),就OK了

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/854633.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号