使用Swagger2时遇到的问题
Swagger2使用起来很简单,加一个@EnableSwagger2注解,并引入如下依赖就ok了
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
配置好之后,启动项目,浏览器输入 http://localhost:8080/swagger-ui.html 应该就能看到api页面了。
But…
问题一:认证
Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually:
问题原因:项目中使用了Spring Security,swagger2相关的资源没做免登录配置
解决办法:增加如下配置,把Swagger2相关的请求都允许
@Component
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
// allow Swagger URL to be accessed without authentication
web.ignoring().antMatchers(
"/swagger-ui.html",
"/v2/api-docs", // swagger api json
"/swagger-resources/configuration/ui", // 用来获取支持的动作
"/swagger-resources", // 用来获取api-docs的URI
"/swagger-resources/configuration/security", // 安全选项
"/swagger-resources/**"
);
}
}
问题二:页面提示undifined
解决认证问题之后,打开swagger-ui,页面是出来了,不过页面提示
fetching resource list: http://localhost:8080undefined;
api 选择下拉框里面也是undefined,浏览器f12有js报错
Uncaught DOMException: Failed to execute 'open' on 'XMLHttpRequest': Invalid URL
at h.end (http://localhost:8080/webjars/springfox-swagger-ui/swagger-ui.min.js:13:5344)
at u.execute (http://localhost:8080/webjars/springfox-swagger-ui/swagger-ui.min.js:7:29443)
at n.exports.c.execute (http://localhost:8080/webjars/springfox-swagger-ui/swagger-ui.min.js:7:27351)
at t.exports.g.build (http://localhost:8080/webjars/springfox-swagger-ui/swagger-ui.min.js:7:17807)
at t.exports.g.initialize (http://localhost:8080/webjars/springfox-swagger-ui/swagger-ui.min.js:7:16198)
at new t.exports (http://localhost:8080/webjars/springfox-swagger-ui/swagger-ui.min.js:7:14664)
at C.n.load (http://localhost:8080/webjars/springfox-swagger-ui/swagger-ui.min.js:13:11513)
at C.n.updateSwaggerUi (http://localhost:8080/webjars/springfox-swagger-ui/swagger-ui.min.js:13:11182)
at C.n. (http://localhost:8080/webjars/springfox-swagger-ui/swagger-ui.min.js:13:10872)
at u (http://localhost:8080/webjars/springfox-swagger-ui/lib/backbone-min.js:1:2091)
页面能出来了,至少说明免认证的配置是生效了。
因为swagger的js已经被压缩过,很难从这段js报错发现问题。本想从网上找下源码,无功而返。
不甘心,从网上下载了自定义的ui,稍作整理后,上传到我的github上了
这个自定义的有2个版本,只改了UI,没有改java代码逻辑,
访问地址分别是
- swagger/index.html
- swagger-v2/docs.html
将这2个地址加入过滤,然后方式试了下,结果发现
swagger/index.html 可以正常访问
swagger-v2/docs.html 也有js报错
有错爆出来,又有源码,就好办了。查看了下js报错的位置,是调用了 /v2/api-docs 后处理返回json数据报错了。
直接在浏览器里面访问了下 /v2/api-docs ,发现回来的数据直接是{“error_no”:“0”,“error_info”:“”}
这里应该不对了,返回的怎么会是这个呢?
debug跟踪一下,springfox.documentation.swagger2.web.Swagger2Controller#getDocumentation方法处理完之后,返回的是
return new ResponseEntity<Json>(jsonSerializer.toJson(swagger), HttpStatus.OK);
// 其中JSON对象代码如下
package springfox.documentation.spring.web.json;
import com.fasterxml.jackson.annotation.JsonRawValue;
import com.fasterxml.jackson.annotation.JsonValue;
public class Json {
private final String value;
public Json(String value) {
this.value = value;
}
@JsonValue
@JsonRawValue
public String value() {
return value;
}
}
而且这里的json数据还是正常的,继续往下执行,发现进入到了项目自定义的ResponseBodyAdvice中。在这个增强器中,对返回的数据都统一加了error_no和error_info。
继续debug发现,这个方法里面把body转成了map,而在转换的时候,得到的是空的map!!??
那为什么明明有数据的body对象,转map就成空的了呢?
// object转map方法核心代码摘录如下
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (StringUtils.equalsIgnoreCase(key, "class")) {
continue;
}
Method getter = property.getReadMethod();
Object value = getter != null ? getter.invoke(obj) : null;
}
原来,beanInfo.getPropertyDescriptors()获取属性描述的时候,只能拿到 getXXX,setXXX方法。而上面的Json对象的value字段刚好没有getValue方法。
使用Swagger2时遇到的问题的更多相关文章
- Shiro:整合swagger2时需要放行的资源
filterMap.put("/swagger-ui.html", "anon"); filterMap.put("/swagger-resource ...
- Swagger2限定接口范围
前面在使用Swagger2时遇到的坑中简单介绍了Swagger的使用. 不过默认情况下,Swagger2会把项目中的所有接口都展示在列表里,特别是你用了Springboot/SpringCloud之后 ...
- 第08章—整合Spring Data JPA
spring boot 系列学习记录:http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址:https://gitee.com/jinxia ...
- windows类书的学习心得(转载)
原文网址:http://www.blogjava.net/sound/archive/2008/08/21/40499.html 现在的计算机图书发展的可真快,很久没去书店,昨日去了一下,真是感叹万千 ...
- 从 Paxos 到 ZooKeeper
分布式一致性 分布式文件系统.缓存系统和数据库等大型分布式存储系统中,分布式一致性都是一个重要的问题. 什么是分布式一致性?分布式一致性分为哪些类型?分布式系统达到一致性后将会是一个什么样的状态? 如 ...
- 使用swagger2配置springboot时出现的问题
这个问题踩了几次坑了,这次又遇到了,不记录一下看来是不长记性了: 测试普通的增删改查的时候,发现删除和查询是对的,可是增加和更新却数据绑定不到controller的参数上面去. 因为是自定义的实体类, ...
- Spring MVC中使用 Swagger2 构建Restful API
1.Spring MVC配置文件中的配置 [java] view plain copy <!-- 设置使用注解的类所在的jar包,只加载controller类 --> <contex ...
- 基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建
基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建 前言 最近做回后台开发,重新抓起以前学过的SSM(Spring+Sp ...
- Spring MVC 中使用 Swagger2 构建动态 RESTful API
当多终端(WEB/移动端)需要公用业务逻辑时,一般会构建 RESTful 风格的服务提供给多终端使用. 为了减少与对应终端开发团队频繁沟通成本,刚开始我们会创建一份 RESTful API 文档来记录 ...
随机推荐
- jdk8中关于操作集合的一些新特性,遍历和排序操作
jdk8增加了不少新的东西,在集合操作这块,就有如 lamda表达式,stream,sort,optional等新的类,主要涉及遍历和排序等方面,新特性提升了不少性能,我们开发就是要拥抱新事物,守着老 ...
- java技术突破要点
一.源码分析 源码分析是一种临界知识,掌握了这种临界知识,能不变应万变,源码分析对于很多人来说很枯燥,生涩难懂. 源码阅读,我觉得最核心有三点:技术基础+强烈的求知欲+耐心. 我认为是阅读源码的最核心 ...
- verilog HDL-并行语句之assign
线网型数据对象: 是verilog hdl常用数据对象之一,起到电路节点之间的互联作用,类似于电路板上的导线. wire是verilog hdl默认的线网型数据对象. 线网型数据对象的读操作在代码任何 ...
- Anton 上课题
Anton 上课题 Anton likes to play chess. Also he likes to do programming. No wonder that he decided to a ...
- python 引入本地 module
数据校验时,需要引入本地的一个告警python代码,引入的方式如下: import sys import os # 引入本地文件目录,或和需要引入的python文件放在同一个文件夹下 sys.path ...
- 《Pro git》
可以通过阅读 CODING 工程师参与翻译的 <Pro Git> 进一步掌握 Git 版本控制系统. https://git-scm.com/book/zh/v2
- Android开发 - 掌握ConstraintLayout(一)传统布局的问题
在传统的Android开发中,页面布局占用了我们很多的开发时间,而且面对复杂页面的时候,传统的一些布局会显得非常复杂,每种布局都有特定的应用场景,我们通常需要各种布局结合起来使用来实现复杂的页面.随着 ...
- 电子技术经典资料汇总:PCB设计篇
电子技术经典资料汇总:PCB设计篇,下面的链接是一个一个的文件下载的,也是压缩包的内容,只不过我把他们给汇总成了一个压缩包,方便大家下载,还有更多电子技术必备基础资料,通信无线类的,C语言篇的,关于电 ...
- Linux pwn入门教程(6)——格式化字符串漏洞
作者:Tangerine@SAINTSEC 0x00 printf函数中的漏洞 printf函数族是一个在C编程中比较常用的函数族.通常来说,我们会使用printf([格式化字符串],参数)的形式来进 ...
- 第十九节:Java基本数据类型,循环结构与分支循环
基本数据类型 Java中的基本数据类型,有8种,在Java中有四种类型,8种基本数据类型. 字节 boolean 布尔型为1/8 byte 字节类型为1 short 短整型为2 char 字符型为2 ...