1. 程式人生 > 實用技巧 >異常解決:swagger2.9.2 報java.lang.NumberFormatException: For input string: ““...

異常解決: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;
        }
    }