swagger2 2.9.2 訪問 swagger-ui.html 頁面時, 服務器拋異常


1. 問題描述:

項目中配置 swagger2 2.9.2 , 啟動項目, 當訪問 swagger-ui.html 頁面時, 服務器拋異常:

 

io.swagger.models.parameters.AbstractSerializableParameter [421] -| 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.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

2.報錯原因

這是由於實體類使用@ApiModelProperty時,example屬性沒有賦值導致的,在AbstractSerializableParameter的getExample方法中會將數值屬性的example的轉換數值類返回,example的默認值是"",因此當example沒有賦值時,會出現上面的異常。getExample方法如下

 1  @JsonProperty("x-example")
 2     public Object getExample() {
 3         if (example == null) {
 4             return null;
 5         }
 6         try {
 7             if (BaseIntegerProperty.TYPE.equals(type)) {
 8                 return Long.valueOf(example);
 9             } else if (DecimalProperty.TYPE.equals(type)) {
10                 return Double.valueOf(example);
11             } else if (BooleanProperty.TYPE.equals(type)) {
12                 if ("true".equalsIgnoreCase(example) || "false".equalsIgnoreCase(defaultValue)) {
13                     return Boolean.valueOf(example);
14                 }
15             }
16         } catch (NumberFormatException e) {
17             LOGGER.warn(String.format("Illegal DefaultValue %s for parameter type %s", defaultValue, type), e);
18         }
19         return example;
20     }

3. 解決辦法:

<swagger-models.version>1.5.22</swagger-models.version>
<swagger2.version>2.9.2</swagger2.version>
 <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger2.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger2.version}</version>

<!--排除swagger2的annotations和models依賴,然后再引入1.5.21版本的annotations和models依賴--> <exclusions> <exclusion> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> <version>${swagger-models.version}</version> </dependency>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM