异常解决:swagger2.9.2 报java.lang.NumberFormatException: For input string: ““...

swagger2.9.2 报java.lang.NumberFormatException: For input string: ““...


springfox-swagger 2.9.2 内置的swagger-models1.5.20 会引起Long类型格式转换异常,报错如下

java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_181]
    at java.lang.Long.parseLong(Long.java:601) ~[na:1.8.0_181]
    at java.lang.Long.valueOf(Long.java:803) ~[na:1.8.0_181]
    at io.swagger.models.parameters.AbstractSerializableParameter.getExample(AbstractSerializableParameter.java:412) ~[swagger-models-1.5.20.jar:1.5.20]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]

这段报错的意思是,在将空串"",转换为Long类型时出现异常

解决方法一

对Long类型上的参数或属性上使用swagger注解时,指定example的值为数值型,比如

@ApiModelProperty(value = "公园",example="1", required = true)
private Long parkId;

具体原因:

项目中很多地方都使用了swagger注解,但对Long类型添加注解时未指定example值,比如

//-------某个字段是Long类型的 这里未指定example的值,默认是空串 ""
    @ApiModelProperty(value = "公园", required = true)
    private Long parkId;
进入AbstractSerializableParameter类查看 getExample()方法
@JsonProperty("x-example")
    public Object getExample() {
        // 如果example未指定值,默认是 example=""
        if (example == null) {
            return null;
        }
        try {
            // 这里的 equals():只要参数不为null,并且是一个String类型,就返回true.
            // 判断被注解(属性,参数)的类型,进入第一个if内。 Long.valueOf("")-->抛出NumberFormatException异常被捕获
            if (BaseIntegerProperty.TYPE.equals(type)) {
                return Long.valueOf(example);
            } else if (DecimalProperty.TYPE.equals(type)) {
                return Double.valueOf(example);
            } else if (BooleanProperty.TYPE.equals(type)) {
                if ("true".equalsIgnoreCase(example) || "false".equalsIgnoreCase(defaultValue)) {
                    return Boolean.valueOf(example);
                }
            }
        } catch (NumberFormatException e) {
            LOGGER.warn(String.format("Illegal DefaultValue %s for parameter type %s", defaultValue, type), e);
        }
        return example;
    }
复现异常
public static void main(String[] args) {
        long l = Long.valueOf("");
        System.out.println(l);
}
// 报错
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Long.parseLong(Long.java:601)
    at java.lang.Long.valueOf(Long.java:803)
    at com.lianxin.werm.api.resource.room.RoomForm.main(RoomForm.java:124)

解决方法二

将swagger-models1.5.20 版本升到1.5.22

<dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.9.2</version>
                <!-- 排除自带的1.5.20版本-->
                <exclusions>
                    <exclusion>
                        <groupId>io.swagger</groupId>
                        <artifactId>swagger-models</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!-- 使用1.5.22-->
            <dependency>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-models</artifactId>
                <version>1.5.22</version>
            </dependency>

新版对这个问题进行了修复,查看getExample()

如果example="" ,直接返回

@JsonProperty("x-example")
    public Object getExample() {
        // example不为null 同时不为空串          这里的isEmpty():判断是否是空串
        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 {
            // 如果example="" 直接返回
            return this.example;
        }
    }
原文地址:https://www.cnblogs.com/haolb123/p/14183839.html