zoukankan      html  css  js  c++  java
  • swagger2打开doc页面时报错

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>

    我是用的swagger2版本是2.9.2,而此版本的swagger2引用的swagger基础包版本是1.5.20

    [INFO] +- io.springfox:springfox-swagger2:jar:2.9.2:compile
    [INFO] |  +- io.swagger:swagger-annotations:jar:1.5.20:compile
    [INFO] |  +- io.swagger:swagger-models:jar:1.5.20:compile

    如果在Integer类型的属性上使用@ApiModelProperty注解,并且没有写example默认值,就会报如下错误:

    2019-10-10 17:28:58.214  WARN 7037 --- [http-nio-8011-exec-129] i.s.m.p.AbstractSerializableParameter    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.GeneratedMethodAccessor794.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)

    从日志来看,我们可以到类AbstractSerializableParameter,下面的方法getExample 一探究竟。

    swagger-models-1.5.20.jar版本的getExample源码如下:this.example是String类型,默认是"", 所以在执行到 return Long.valueOf(this.example) 这行代码是就会报错。

    @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;
        }
    }

    解决方法有2个:

    1. 把Integer的属性@ApiModelProperty注解里都加上example,内容必须可以转为数字,比如:@ApiModelProperty(value = "年龄", example = "20")

    2. 把swagger版本升级为1.5.21,下面是swagger-models-1.5.21.jar的源码:

    @JsonProperty("x-example")
    public Object getExample() {
        if (this.example != null && !this.example.isEmpty()) {
            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;
        } else {
            return this.example;
        }
    }

    增加了!this.example.isEmpty() 的判断,isEmply()的源码如下:

    public boolean isEmpty() {
        return value.length == 0;
    }
  • 相关阅读:
    利用avalon 实现一个简单的成绩单
    有关less 处理@arguments的一些高级技巧
    迷你MVVM框架 avalonjs 0.9发布
    IE9-10 option BUG
    判定元素正在插入到DOM树——DOMNodeInsertedIntoDocument
    迷你MVVM框架 avalonjs 0.85发布
    机器学习研究与开发平台的选择
    JS生成指定范围内的随机数(支持随机小数)
    DDD领域驱动设计 ---- 系列文章
    序列化效率比拼——谁是最后的赢家avaScriptSerializer方式、DataContract方式、Newtonsoft.Json
  • 原文地址:https://www.cnblogs.com/huanshilang/p/11649607.html
Copyright © 2011-2022 走看看