swagger2打开doc页面时报错

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

我是用的swagger2版本是2.9.2,而此版本的swagger2引用的swagger基础包版本是1.5.20

[INFO] +- io.springfox:springfox-swagger2:jar:2.9.2:compile
[INFO] |  +- io.swagger:swagger-annotations:jar:1.5.20:compile
[INFO] |  +- io.swagger:swagger-models:jar:1.5.20:compile

如果在Integer类型的属性上使用@ApiModelProperty注解,并且没有写example默认值,就会报如下错误:

2019-10-10 17:28:58.214  WARN 7037 --- [http-nio-8011-exec-129] i.s.m.p.AbstractSerializableParameter    Illegal DefaultValue null for parameter type integer
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 io.swagger.models.parameters.AbstractSerializableParameter.getExample(AbstractSerializableParameter.java:412)
    at sun.reflect.GeneratedMethodAccessor794.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

从日志来看,我们可以到类AbstractSerializableParameter,下面的方法getExample 一探究竟。

swagger-models-1.5.20.jar版本的getExample源码如下:this.example是String类型,默认是"", 所以在执行到 return Long.valueOf(this.example) 这行代码是就会报错。

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

 

解决方法有2个:

1. 把Integer的属性@ApiModelProperty注解里都加上example,内容必须可以转为数字,比如:@ApiModelProperty(value = "年龄", example = "20")

2. 把swagger版本升级为1.5.21,下面是swagger-models-1.5.21.jar的源码:

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

增加了!this.example.isEmpty() 的判断,isEmply()的源码如下:

public boolean isEmpty() {
    return value.length == 0;
}

 

posted @ 2019-10-10 17:51  闲人鹤  阅读(2636)  评论(2编辑  收藏  举报