问题:
swagger 的 2.9.2 版本内部默认依赖的是:
-
swagger-annotations:1.5.20
-
swagger-models:1.5.20
但这个版本在部分场景(比如和 Spring Boot 2.x / springfox-bootstrap-ui 等)集成时,可能存在以下问题:
💥 常见报错场景
-
Swagger UI 加载失败,控制台报错:
swagger For input string: ""
说明有字段被错误地序列化为
""
空字符串,而旧版本不兼容这种格式。 -
某些注解不生效,比如 @ApiModelProperty,导致生成文档缺字段。
-
与 swagger-bootstrap-ui 插件版本不兼容。
解决的代码:
<!-- swagger2 配置 -->
<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>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- 用于解决刷新 swagger-ui 页面,控制台报错 swagger For input string: "" 的问题 -->
<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>