swagger2:Illegal DefaultValue null for parameter type integer

代码

@GetMapping("/goodsDetail")
@ApiOperation("商品详情")
public Result goodsDetail(@RequestParam @ApiParam(value = "商品 id", required = true) Long goodsId,
                          @RequestParam @ApiParam(value = "推广位 id", required = true) String pId) throws Exception {
    String goodsDetail = pddService.goodsDetail(goodsId, pId);
    return new Result().ok(goodsDetail);
}

报错信息

WARN i.s.m.p.AbstractSerializableParameter :Illegal DefaultValue null for parameter type integer

报错路径信息

java.lang.NumberFormatException: For input string: ""
	at io.swagger.models.parameters.AbstractSerializableParameter
         .getExample(AbstractSerializableParameter.java:412)

查看源码

由于 example 的默认值为 “”java

String example() default "";

将空字符串转为 Long 类型时报错bash

@JsonProperty("x-example")
public Object getExample() {
    if (this.example == null) {
        return null;
    } else {
        try {
            if ("integer".equals(this.type)) {
                return Long.valueOf(this.example);
            }

            if ("number".equals(this.type)) {
                return Double.valueOf(this.example);
            }

            if ("boolean".equals(this.type) && ("true".equalsIgnoreCase(this.example) || "false".equalsIgnoreCase(this.defaultValue))) {
                return Boolean.valueOf(this.example);
            }
        } catch (NumberFormatException var2) {
            LOGGER.warn(String.format("Illegal DefaultValue %s for parameter type %s", this.defaultValue, this.type), var2);
        }

        return this.example;
    }
}

修改方案

给 Long 类型的参数添加数值类型的 exampleapp

@GetMapping("/goodsDetail")
@ApiOperation("商品详情")
public Result goodsDetail(@RequestParam @ApiParam(value = "商品 id", example = "0", required = true) Long goodsId,
                          @RequestParam @ApiParam(value = "推广位 id", required = true) String pId) throws Exception {
    String goodsDetail = pddService.goodsDetail(goodsId, pId);
    return new Result().ok(goodsDetail);
}

总结

在使用 swagger 时须要注意数值类型的参数(integer、long)须要添加一个 example 属性ui