解决/null/swagger-resources异常,Swagger2.x升级到3.0后的若干问题

方案一

解决/null/swagger-resources异常

在请求经过nginx的时候就过滤掉。

        location ^~ /null/ {
           deny all;
        }

治标不治本,根本无法ip访问

方案二

升级到swagger3.0

第一个问题ApiKey无法转换为SecurityScheme

对应报错

java: 不兼容的类型: java.util.List<springfox.documentation.service.ApiKey>无法转换为java.util.List<springfox.documentation.service.SecurityScheme>

对应代码

//设置的认证头
docket.securitySchemes(securitySchemes()).securityContexts(securityContexts());
//调用的方法
    private List<ApiKey> securitySchemes() {
        //3.0改变了返回结果
        List<ApiKey> result = new ArrayList<>();
        ApiKey apiKey = new ApiKey("ApiKeyAuth", "Authorization", "header");
        result.add(apiKey);
        return result;
    }

这个报错很明显,说ApiKey无法转换为SecurityScheme,然后我看了下3.0和2.7的源码,3.0版本docket.securitySchemes的方法目前只支持SecurityScheme,2.7版本docket.securitySchemes的方法支持SecurityScheme的子类
解决办法
修改自定义方法的返回类型securitySchemes() 

    private List<SecurityScheme> securitySchemes() {
        //3.0改变了返回结果
        List<SecurityScheme> result = new ArrayList<>();
        ApiKey apiKey = new ApiKey("ApiKeyAuth", "Authorization", "header");
        result.add(apiKey);
        return result;
    }

第二个问题访问http://localhost:8081/swagger-ui.html报404

解决办法
对于3.0的swagger-ui,使用以下maven坐标替换springfox-swagger-ui包

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

访问路径为http://localhost:8081/swagger-ui/index.html

第三个问题访问swagger页面报错Unable to render this definition

翻译过来就是版本不匹配,需要在代码中设置为3.0版本​​​​​​​

 

解决办法
可以看下代码中创建的版本是否正确, swagger源码的版本目前有三个值,对于3.0版本要选择OAS_30​​​​​​​​​​​​​​

注意

对于配置了Security的项目,需要注意swagger-ui对应url资源放行

      - /swagger-ui/
      - /swagger-resources/**
      - /swagger/**
      - /**/v3/api-docs/**
      - /**/*.js
      - /**/*.css
      - /**/*.png
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值