美文网首页
Swagger 访问异常 控制台报错

Swagger 访问异常 控制台报错

作者: hare426 | 来源:发表于2020-03-17 16:37 被阅读0次
环境
  • swagger版本: 2.9.2

  • maven信息:

         <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>
异常信息
Illegal DefaultValue  for parameter type integer 
java.lang.NumberFormatException: For input string: ""
log
    <!--swagger start-->
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-annotations</artifactId>
        <version>1.5.22</version>
    </dependency>

    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-models</artifactId>
        <version>1.5.22</version>
    </dependency>
    <!--swagger end-->
处理办法
  • 添加maven配置:
        <!--swagger start-->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.22</version>
        </dependency>

        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.5.22</version>
        </dependency>
        <!--swagger end-->
原理

1.5.20 源码

@JsonProperty("x-example")
   public Object getExample() {
       if (this.example == null) {
           return null;
       } else {
           try {
               if ("integer".equals(this.type)) {
                   return Long.valueOf(this.example);
               }

               if ("number".equals(this.type)) {
                   return Double.valueOf(this.example);
               }

               if ("boolean".equals(this.type) && ("true".equalsIgnoreCase(this.example) || "false".equalsIgnoreCase(this.defaultValue))) {
                   return Boolean.valueOf(this.example);
               }
           } catch (NumberFormatException var2) {
               LOGGER.warn(String.format("Illegal DefaultValue %s for parameter type %s", this.defaultValue, this.type), var2);
           }

           return this.example;
       }
   }

1.5.22 源码

@JsonProperty("x-example")
    public Object getExample() {
        if (this.example != null && !this.example.isEmpty()) {
            try {
                if ("integer".equals(this.type)) {
                    return Long.valueOf(this.example);
                }

                if ("number".equals(this.type)) {
                    return Double.valueOf(this.example);
                }

                if ("boolean".equals(this.type) && ("true".equalsIgnoreCase(this.example) || "false".equalsIgnoreCase(this.defaultValue))) {
                    return Boolean.valueOf(this.example);
                }
            } catch (NumberFormatException var2) {
                LOGGER.warn(String.format("Illegal DefaultValue %s for parameter type %s", this.defaultValue, this.type), var2);
            }

            return this.example;
        } else {
            return this.example;
        }
    }

分析:从上面的代码可以看出对example的判断是不同的,增加了当example为空的时候,直接返回example,所以不会再报错。

注:

原理部分引用自:Swagger2.9.2的NumberFormatException - 简书

相关文章

网友评论

      本文标题:Swagger 访问异常 控制台报错

      本文链接:https://www.haomeiwen.com/subject/kjwtyhtx.html