swagger2使用遇到的坑

    1,swagger-ui.html页面测试接口时,报如下错误:
       TypeError: NetworkError when attempting to fetch resource fetch
       说明a) 单独访问该接口正常
              b) 地址服务器访问swagger-ui.html 正常
              c) 使用域名访问报错,切不同浏览器错误描述不一致
       原因:swagger跨域问题
       解决方案:nginx做代理内网域名时,
       增加反向代理-做跨域支持
       location /apis {
            rewrite  ^.+apis/?(.*)$ /$1 break;
            include  uwsgi_params;
               proxy_pass   http://server:port;
       }
      配置文件中增加swagger访问域名
      springfox.documentation.swagger.v2.host = xxx.xxx.com(.cn)


    2,访问swagger-ui.html页面,一直弹出一个框:
        Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually:
      这个错误,有多很多种情况
      1),没有增加@EnableSwagger2 启用swagger的配置
         解决方案:SwaggerConfig上增加@EnableSwagger2,启用swagger的配置
      2),被类似shiro的权限控制组件拦截
         解决方案:过滤swagger页面的权限控制
                     /swagger-ui.html
                     /webjars/**
                     /swagger-resources/**
                     /v2/**
      3),以上信息都配置了,仍然报错。经测试:@EnableSwagger2配置更换到启动类上可正常访问
           但这不是肯定不是根本原因。后来经过比对配置文件,发现SwaggerConfig类上的注解
           @ConditionalOnProperty(prefix = "xxx", name = "yyy", havingValue = "true")修改了
           对于springboot熟悉的人。看到这个可能已经知道原因了。@ConditionalOnProperty注解控制此类上的其他注解是否生效
            解决方案:修改配置文件中的xxx.yyy和配置文件(配置中心)的一致

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值