一、異常分析:
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 = "1") private int pageId;
方法二:
忽略原版本的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>