Swagger2使用出现NumberFormatException

一、问题

2019-12-12 13:59:55.35  WARN 19699 --- [nio-1111-exec-4] 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) ~[na:1.8.0_171]
	at java.lang.Long.parseLong(Long.java:601) ~[na:1.8.0_171]
	at java.lang.Long.valueOf(Long.java:803) ~[na:1.8.0_171]
	at io.swagger.models.parameters.AbstractSerializableParameter.getExample(AbstractSerializableParameter.java:412) ~[swagger-models-1.5.20.jar:1.5.20]
	at sun.reflect.GeneratedMethodAccessor109.invoke(Unknown Source) ~[na:na]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_171]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_171]
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:687) [jackson-databind-2.9.6.jar:2.9.6]

二、分析

at io.swagger.models.parameters.AbstractSerializableParameter.getExample(AbstractSerializableParameter.java:412) ~[swagger-models-1.5.20.jar:1.5.20]

根据上面这句报错信息,点进去AbstractSerializableParameter.java:412可以定位到getExample(),同时定位到引入依赖为swagger-models的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;
        }
    }

就是说如果实体属性类型是Integer,就把example转为Long类型,而example默认为"",导致转换错误。

三、解决

3.1、方法一:@ApiModelProperty

实体类中,Integer类型的属性加@ApiModelProperty时,必须要给example参数赋值,且值必须为数字类型。

public class User {
    @ApiModelProperty(value = "用户ID",example = "123")
    private Integer id;
}

3.2、方法二:添加1.5.22依赖

上面获取了[swagger-models-1.5.20.jar:1.5.20]对应的默认版本为1.5.20,把对于的依赖单独添加为1.5.22

 <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>

查看对应的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;
        }
    }

添加了代码return this.example;,相当于了当example为空的时候,直接返回example,所以不会再报错。

发布了132 篇原创文章 · 获赞 150 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_35988274/article/details/103507954
今日推荐