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

swagger2打开doc页面时报错的更多相关文章

  1. ecshop安装后打开管理页面时报500错误

    昨天给朋友安装ecshop,遇到如下问题: 1.PHP不支持mysql扩展 打开http://localhost/install/index.php,第二步时候,报不支持mysql. ecshop是前 ...

  2. error while obtaining ui hierarchy xml file...用 uiautomatorviewer 获取安卓手机软件页面时报错

    Error while obtaining UI hierarchy XML file: com.android.ddmlib.SyncException: Remote object doesn't ...

  3. 火狐浏览器调试ajax异步页面时报错NS_ERROR_UNEXPECTER

    第一个直观的结论就是ajax调用出错,如果其他浏览器却调用没报错,而且正常返回值了,那么就是Firefox浏览器的问题了: 如果其他浏览器也没余完全正常执行,而是出现和我上一篇ajax向后台请求数据, ...

  4. vue-cli3使用vue-router 使用动态路由,在刷新页面时报错

    刚发现的一个问题,在使用vue-cli3创建项目之后,使用动=动态路由,demo: { path: '/aa/:id', name: 'aa', component: aa }, 编程式路由: thi ...

  5. tomcat8.5打开manager页面报错的问题

    之前用的8.0版本的tomcat,最近需要将版本升级,当前8的最新的版本是8.5.42,升级之后发现manager页面打不开了,就是下面这个按钮的页面 点击之后报403没权的错误 还是按照8.0版本的 ...

  6. vc6.0打开类向导时报错-Parsing error: Expected ";".Input Line: "解决方法

    --------------------------- Microsoft Visual C++ --------------------------- Parsing error:  Expecte ...

  7. EF:打开Oracle连接时报错

    基础提供程序在 Open 上失败. The underlying provider failed on Open. 解决:安装最新的ODTwithODAC121024.

  8. tomcat已启动,使用maven的deploy发布后,根据路径打开浏览器访问时报错HTTP Status 500 - Error instantiating servlet class

    web项目中请求出现错误,如下: HTTP Status 500 - Error instantiating servlet class XXXX类 type Exception report mes ...

  9. vue 访问页面时报错 Failed to compile

    这个是因为node-sass没安装好,所以要重新安装 windows下运行命令:npm install node-sass --registry=https://registry.npm.taobao ...

随机推荐

  1. 洛谷CF997A:Convert to Ones

    温馨提示: 本题适合先思考再看题解,相信各位神犇都能轻轻松松过掉它. 题目链接: https://www.luogu.com.cn/problem/CF997A 分析: 首先要读懂题,to ones, ...

  2. plus.runtime.quit()是个好函数

    对于H5+APP开发,应用的生命周期监听函数里是对应用行为的监控,但是并不对应用执行退出或重启操作.相关操作还是要使用mui

  3. day31 反射,内置方法,元类

    目录 一.反射 1 什么是反射 2 如何实现反射 二.内置方法 1 什么是内置方法 2 为什么要用内置方法 3 如何使用内置方法 3.1 str 3.2 del 三.元类 1 什么是元类 2 clas ...

  4. (三)pandas 层次化索引

    pandas层次化索引 1. 创建多层行索引 1) 隐式构造 最常见的方法是给DataFrame构造函数的index参数传递两个或更多的数组 Series也可以创建多层索引 import numpy ...

  5. linux 安装 mysql8

    1. 下载地址: https://dev.mysql.com/downloads/file/?id=484922 2. 安装 mysql80-community-release-el7-3.noarc ...

  6. Ethical Hacking - NETWORK PENETRATION TESTING(14)

    MITM - ARP Poisoning Theory Man In The Middle Attacks - ARP Poisoning This is one of the most danger ...

  7. PJzhang:python基础入门的7个疗程-seven

    猫宁!!! 参考链接:易灵微课-21天轻松掌握零基础python入门必修课 https://www.liaoxuefeng.com/wiki/1016959663602400 第19天:开源模块 数据 ...

  8. 题解 洛谷 P4336 【[SHOI2016]黑暗前的幻想乡】

    生成树计数的问题用矩阵树定理解决. 考虑如何解决去重的问题,也就是如何保证每个公司都修建一条道路. 用容斥来解决,为方便起见,我处理时先将\(n\)减了1. 设\(f(n)\)为用\(n\)个公司,且 ...

  9. Python新手学习raise用法

    当程序出现错误时,系统会自动引发异常.除此之外,Python也允许程序自行引发异常,自行引发异常使用 raise 语句来完成. 很多时候,系统是否要引发异常,可能需要根据应用的业务需求来决定,如果程序 ...

  10. python3的字符串常用方法

    find()# 方法 find()# 范围查找子串,返回索引值,找不到返回-1 # 语法 s.find(substring, start=0, end=len(string)) # 参数 # subs ...