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时遇到的问题的更多相关文章

  1. Shiro:整合swagger2时需要放行的资源

    filterMap.put("/swagger-ui.html", "anon"); filterMap.put("/swagger-resource ...

  2. Swagger2限定接口范围

    前面在使用Swagger2时遇到的坑中简单介绍了Swagger的使用. 不过默认情况下,Swagger2会把项目中的所有接口都展示在列表里,特别是你用了Springboot/SpringCloud之后 ...

  3. 第08章—整合Spring Data JPA

    spring boot 系列学习记录:http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址:https://gitee.com/jinxia ...

  4. windows类书的学习心得(转载)

    原文网址:http://www.blogjava.net/sound/archive/2008/08/21/40499.html 现在的计算机图书发展的可真快,很久没去书店,昨日去了一下,真是感叹万千 ...

  5. 从 Paxos 到 ZooKeeper

    分布式一致性 分布式文件系统.缓存系统和数据库等大型分布式存储系统中,分布式一致性都是一个重要的问题. 什么是分布式一致性?分布式一致性分为哪些类型?分布式系统达到一致性后将会是一个什么样的状态? 如 ...

  6. 使用swagger2配置springboot时出现的问题

    这个问题踩了几次坑了,这次又遇到了,不记录一下看来是不长记性了: 测试普通的增删改查的时候,发现删除和查询是对的,可是增加和更新却数据绑定不到controller的参数上面去. 因为是自定义的实体类, ...

  7. Spring MVC中使用 Swagger2 构建Restful API

    1.Spring MVC配置文件中的配置 [java] view plain copy <!-- 设置使用注解的类所在的jar包,只加载controller类 --> <contex ...

  8. 基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建

    基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建 前言 最近做回后台开发,重新抓起以前学过的SSM(Spring+Sp ...

  9. Spring MVC 中使用 Swagger2 构建动态 RESTful API

    当多终端(WEB/移动端)需要公用业务逻辑时,一般会构建 RESTful 风格的服务提供给多终端使用. 为了减少与对应终端开发团队频繁沟通成本,刚开始我们会创建一份 RESTful API 文档来记录 ...

随机推荐

  1. 内置函数_map()、reduce()、filter()

    map().reduce().filter() map()内置函数把一个函数func依次映射到序列或迭代器对象的每个元素上,并返回一个可迭代的map对象作为结果,map对象中每个元素是原序列中元素经过 ...

  2. HashSet源码

    public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, java. ...

  3. rabbitMq 初步

    RabbitMQ的工作原理 它的基本结构 组成部分说明如下: Broker:消息队列服务进程,此进程包括两个部分:Exchange和Queue. Exchange:消息队列交换机,按一定的规则将消息路 ...

  4. 软件包管理之rpm与yum

    软件包的安装和卸载时很平常的事,但在Linux上面却不简单..Linux的其中一个哲学就是一个程序只做一件事,并且做好.组合小程序来完成复杂的任务,这样做有很多好处,但是各个小程序之间往往会存在着复杂 ...

  5. H5拖动火狐自动打开新标签

    写在前面的话:<H5拖动火狐自动打开新标签>原因是为什么百度很多了我就不细说,本文章只说我自己的解决方法... 自定义数据里写个链接,如果火狐自动打开就跳到这个链接 这个页面就写一句话:关 ...

  6. python 旋转数组 多种解题思路

    leetcode 题目描述:给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 尽可能想出更多的解决方案,至少有三种不同的方法可以解决这个问题. 要求使用空间复杂度为 O(1) 的 ...

  7. python之线程(threading)

    线程是属于进程的,一个进程可能包含多个线程 至于线程和进程在使用时哪个更好,只能看使用的场景了 话不多说,看下线程模块(threading)的使用方法: #导入模块 import threading, ...

  8. 一些oracle的经验

    注:再写存储过程的时候,在for循环里要写begin和end,这样就可以写exception ,让这条错误数据回滚,然后记录错误日志,commit 关键字: oracle 存储过程 1.基本结构 CR ...

  9. 在Android Studio添加本地aar包引用

    1.如何在Android Studio添加本地aar包引用 https://jingyan.baidu.com/article/2a13832890d08f074a134ff0.html 2.完成上述 ...

  10. 什么是js的严格模式

    设立严格模式的原因: - 消除Javascript语法的一些不合理.不严谨之处,减少一些怪异行为; - 消除代码运行的一些不安全之处,保证代码运行的安全: - 提高编译器效率,增加运行速度: - 为未 ...