swagger2 Illegal DefaultValue null for parameter type integer
问题,为了方便调试,引入了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,完美解决!
面朝大海```春暖花开

maven的extensions的正确使用方式
浙公网安备 33010602011771号