問題,為了方便調試,引入了swagger2,但是在第一次訪問的時候總是報
Illegal DefaultValue null for parameter type integer
讓人看着很不輸入
定位問題
很明顯說是NumberFormatException
,查看AbstractSerializableParameter的getExample得知
@JsonProperty("x-example")
public Object getExample() {
if (this.example == null) {
return null;
} else {
try {
if ("integer".equals(this.type)) {
return Long.valueOf(this.example);
}
在進行轉化的時候報錯,在this.example == null
判斷的不對。
解決
方式1-修改源碼
將源碼下載下來,進行編輯,把判斷部分修改為if (example == null || example.isEmpty())
之后將其打包上傳到maven私服即可。
弊端,修改源碼只能上傳到私服,麻煩,也比較難
方式2-修改代碼
將
@ApiModelProperty("開始時間,時間戳")
private Long timeBegin;
修改為
@ApiModelProperty(value = "開始時間,時間戳",example = "0")
private Long timeBegin;
很明顯,最容易,但是改動的也不少。
方式3-修改依賴
看網上說在swagger-models
1.5.21
版本修復了該問題
所以要升級版本
exclusions排除依賴
通過maven integration extension
插件將swagger-models
1.5.20
依賴排除
再引入1.5.21
依賴,如下代碼
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
<exclusions>
<exclusion>
<artifactId>swagger-annotations</artifactId>
<groupId>io.swagger</groupId>
</exclusion>
</exclusions>
</dependency>
再查看源碼
ok,完美解決!