<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. 简单了解一下 Swagger

    一.Swagger 1.什么是 Swagger ? Swagger 是一个规范和完整的框架,用于生成.描述.调用以及可视化的 Restful 风格的 Web 服务. 简单的理解:是一款 REST AP ...

  2. 关于jwt6.0.0版本algorithms should be set报错的解决方案

    2020.7.7日jwt更新之后,安装的express-jwt模块会默认为6.0.0版本,我将之前的auth.js文件引入时控制台报错,提示algorithms should be set,中文译为应 ...

  3. AcWing 93. 递归实现组合型枚举

    AcWing 93. 递归实现组合型枚举 原题链接 从 1~n 这 n 个整数中随机选出 m 个,输出所有可能的选择方案. 输入格式 两个整数 n,m ,在同一行用空格隔开. 输出格式 按照从小到大的 ...

  4. ASP.NET Core端点路由 作用原理

    端点路由(Endpoint Routing)最早出现在ASP.NET Core2.2,在ASP.NET Core3.0提升为一等公民. Endpoint Routing的动机 在端点路由出现之前,我们 ...

  5. Kafka常用指令

    工作中经常会用到的指令   # 查询topic为test的partition数量 ./kafka-topics.sh --zookeeper localhost:2181/kafka --topic ...

  6. python发送邮件插件

    github链接:https://github.com/573734817pc/SendEmailPlug-in.git 说明: 1.该插件功能为发送邮件. 2.基于python编写. 3.使用的时候 ...

  7. 一张图就可以完美解决Java面试频次最高、GG最高的题目!快点收藏

    如果要问Java面试频次最高的题目,那么我想应该是HashMap相关了. 提到HahMap,必然会问到是否线程安全?然后牵扯出ConcurrentHashMap等,接着提及1.7和1.8实现上的区分, ...

  8. ref和动态组件

    ref--------指引 另一种获取表单值的方法 是Vue环境中一个内置的属性.它可以使用this.$refs可以快速拿到DOM对象.

  9. scratch编程——画笔模块画各种同心图案

    我们今天是要用画笔来画出不同的同心图案,在画之前,我们先来了解一下画笔模块: 1.画笔模块的用法 画笔模块的用法就是在舞台上留下不同颜色粗细的线条,它的默认是情况是抬笔,我们在使用的时候要让角色移动到 ...

  10. C++语法小记---多重继承

    多重继承 工程中不建议使用多继承,因为多继承带来的问题比带来的便利多,已被放弃 问题一:多重继承的对象,向上获取指针时,有不同的地址 ----无法解决 问题二:菱形继承问题,导致成员冗余 ----虚继 ...