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

记录404的一个坑

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

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.13</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值