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

springboot集成swagger2,解决访问404问题

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

springboot集成swagger2,解决访问404问题

记录404的一个坑 1、pom引入jar,最新的3.0.0版本会报404,有点Bug,所以选择2.9.2

            io.springfox
            springfox-swagger2
            2.9.2
        
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        


        org.springframework.boot
        spring-boot-starter-parent
        2.5.13
         
    
2、新建SwaggerConfig配置类
       package com.llb.wxpaymentdemo.config;



import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@Slf4j
public class Swagger2Config {

    @Bean
    public Docket docket(){
        log.info("Swagger2Config init");
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApiInfo())
                .select() // 要扫描的API(Controller)基础包
                .apis(RequestHandlerSelectors
                        .basePackage("com.llb.wxpaymentdemo.controller"))
                .paths(PathSelectors.any())
                .build();

//                .apiInfo(new ApiInfoBuilder().title("微信支付案例接口文档").build())
//                .genericModelSubstitutes(new Class[]{DeferredResult.class})
//                .select().paths(PathSelectors.any())
//                // 不显示错误的接口地址
                .paths(Predicates.not(PathSelectors.regex("/error.*")))// 错误路径不监控
//                .paths(PathSelectors.regex("/.*"))// 对根下所有路径进行监控
//                .build()
//                ;
    }
    private ApiInfo buildApiInfo() {
        Contact contact = new Contact("开发者","","");
        return new ApiInfoBuilder()
                .title("闪聚支付‐商户应用API文档")
                .description("") .contact(contact)
                .version("1.0.0").build(); }

}


 
3.访问swagger

5 问题总结,如上访问一直404,尝试网上说的各种方法,添加WebMvcConfigurer配置addResourceHandlers都没用

最后发现问题是浏览器上,把浏览器上地址复制下来是:http://localhost:8089/swagger%E2%80%90ui.html

修改地址如下:
http://localhost:8001/swagger-ui.html
立马不再404了,所以根本不需要什么addResourceHandlers

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

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

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