调试一个springboot项目,运行后swagger页面时控制台报警告 Illegal DefaultValue for parameter type integer 报错如下:

WARN i.s.m.p.AbstractSerializableParameter :Illegal DefaultValue null for parameter type integer

报错路径信息:

java.lang.NumberFormatException: For input string: "" at io.swagger.models.parameters.AbstractSerializableParameter .getExample(AbstractSerializableParameter.java:412)

追溯到源码:

@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;
}

}

因为 example 的默认值为 “”

String example() default "";

修改方案

1、将所有给 Long 类型的参数添加数值类型的 example,覆盖原来默认值

@ApiOperation("商品详情")
public Result goodsDetail(@RequestParam @ApiParam(value = "商品 id", example = "0", required = true) Long goodsId) throws Exception {
                         ........
}```

2、彻底解决方案就是更新依赖 将swagger 引入依赖改为

<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> <exclusions> <exclusion> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> </exclusion> <exclusion> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> </exclusion> </exclusions> </dependency> <!-- 解决进入swagger页面报类型转换错误,排除2.9.2中的引用,手动增加1.5.21版本--> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>1.5.21</version> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> <version>1.5.21</version> </dependency>