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 的默认值为 “”

String example() default "";

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

@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 类型的参数添加数值类型的 example

@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 属性

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值