Swagger2异常:java.lang.NumberFormatException:For input string:"",swagger2.9.2访问主页面编译器控制台异常(不影响使用)

哥:经过验证,靠谱。就原样照搬了。

 

复制粘贴过来pom配置,存在编码报错,坑。 

下面是经过验证的。



<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
<exclusions>
<exclusion>
<artifactId>swagger-models</artifactId>
<groupId>io.swagger</groupId>
</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>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.swagger.core.v3/swagger-annotations -->
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.0.8</version>
</dependency>
<!-- http://localhost:18004/doc.html -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.8.8</version>
</dependency>
<!-- Swagger用了高版本的 -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.0-jre</version>
</dependency>

----------------------------------------

Swagger2异常:java.lang.NumberFormatException:For input string:""

 

一、异常分析:

Illegal DefaultValue null for parameter type integer`和`NumberFormatException: For input string: ""

从上面这句可以看出,有个默认值是空字符串的变量转换成Integer类型时异常。

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

根据上面这句报错信息,点进去AbstractSerializableParameter.java:412可以看到

if(BaseIntegerProperty.TYPE.equals(type)){

    return Long.valueOf(example);

}

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

二、解决办法:

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

@ApiModelProperty(value="订单ID",example="123")

方法二:

忽略原版本的swagger-annotations和swagger-models,添加1.5.21版本的

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



作者:平面小狮子
链接:https://www.jianshu.com/p/d08e58cc18ea
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值