记录Swagger出现的问题

今天,在访问项目在线文档,输入http://localhost:8080/swagger-ui.html访问时出现以下问题:

Unable to infer base url. This is common when using dynamic servlet registrantion or when the API is behind an API Gateway...

一、检查Swagger相关的配置:

1、Swagger的依赖引入包,无误。

        <!--    swagger2    -->
        <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>

2、Swagger的Config配置,无误。

package com.mgx.config.swagger;

import io.swagger.annotations.ApiOperation;
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;

/**
 * @author mgx
 * @date 2022/3/14 10:12 AM
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    /**
     * Springfox提供Docket对象,为其设置相关属性,将其注册成为spring的bean后,可以在接口文档中展示
     */
    @Bean
    public Docket getUserDocket(){
        //版本类型是swagger2
        return new Docket(DocumentationType.SWAGGER_2)
                //通过调用自定义方法apiInfo,获得文档的主要信息
                .apiInfo(apiInfo())
                //启动用于api选择的构建器
                .select()
                //指定扫描该包下面的API注解
                .apis(RequestHandlerSelectors.basePackage("com.mgx.controller"))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //路径过滤器(扫描所有路径)
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 创建该API的基本信息(这些基本信息会展现在文档页面中)
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //接口管理文档首页显示
                .title("mgx - 使用Swagger2构建REST-ful APIS ")
                //API的描述
                .description("mgx - 供前后端联调的接口APi文档")
                //项目网站链接
                .termsOfServiceUrl("www.baidu.com")
                //输入想要展示的内容信息
                .contact(new Contact("名字", "联系方式", "邮箱"))
                //版本
                .version("1.0")
                .build();
    }
}

二、配置无误,检查是否为拦截器的问题。

    /**
     * 2:
     */
    @Value(value = "${spring.mvc.servlet.path}")
    private String path;

    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
        // 1.创建过滤器工厂
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        // 2.设置安全管理器
        shiroFilter.setSecurityManager(securityManager);
        // 3.通用配置
        // 3.1配置需要返回登陆提醒的接口
        shiroFilter.setLoginUrl("/mgx/auth/noLogin");
        // 3.2配置未授权的接口
        shiroFilter.setUnauthorizedUrl("/mgx/auth/noPermission");
        // 4.过滤器集合,选择有序集合 key 表示地址  value表示过滤器类型
        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put(path + "/auth/**", "anon");
        filterMap.put(path + "/druid/**", "anon");
        //swagger接口权限 开放
        filterMap.put(path + "/swagger-ui.html", "anon");
        filterMap.put(path + "/webjars/**", "anon");
        filterMap.put(path + "/v2/api-docs", "anon");
        filterMap.put(path + "/swagger-resources/**", "anon");
        // 其他都需要授权认证
        filterMap.put(path + "/**", "authc");
        shiroFilter.setFilterChainDefinitionMap(filterMap);
        return shiroFilter;
    }

找到问题,ShiroConfig配置文件中对资源进行了拦截。访问swagger-ui.html,需要加上配置文件中的根目录。

​http://localhost:8080/mgx/swagger-ui.html​

 总结:经过检查后,发现不是配置有问题,而是输入的url有问题。遇到Swagger问题时先检查配置,配置无误,那么大概率就是拦截器的问题,再去检查拦截器。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值