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. Java 线程使用注意事项

    事件处理线程说明 如果事件处理的逻辑能迅速完成,并且不会发起新的IO请求,比如只是在内存中记个标识,则直接在IO线程上处理更快,因为减少了线程池调度. 但如果事件处理逻辑较慢,或者需要发起新的IO请求 ...

  2. EBS CAS SSO测试

    https://wiki.jasig.org/display/CAS/CASifying+Oracle+Portal https://wenku.baidu.com/view/5f110a85b9d5 ...

  3. 为什么需要micro-service构建平台

    最近一直在做micro-service的开发平台建设.由于这是一个实验项目,目前所有工作都得靠自己操刀. 今天在总结用python开发一个web service时,偶有所得,这让我建设micro-se ...

  4. Container Adaptors

    Notes from C++ Primer stack and queue: based on deque priority_queue:    based on vector Standard li ...

  5. Eclipse 中打包插件 Fat Jar 的安装与使用

    Eclipse可以安装一个叫Fat Jar的插件,用这个插件打包非常方便,Fat Jar的功能非常强大. 首先要下载Fat Jar,下载地址:https://sourceforge.net/proje ...

  6. Android之控件与布局,结构知识点,基础完结

    版权声明:未经博主允许不得转载 在Android中我们常常用到很多UI控件,如TextView,EditText,ImageView,Button,ImageButton,ToggleButton,C ...

  7. Swift5 语言指南(十一) 结构和类

    结构和类是通用的,灵活的结构,它们成为程序代码的构建块.您可以使用与定义常量,变量和函数相同的语法来定义属性和方法,以便为结构和类添加功能. 与其他编程语言不同,Swift不要求您为自定义结构和类创建 ...

  8. iOSAPP开发项目搭建

    架构图: 架构原则:易读性.易维护性.易扩展性. 一.思考 做好一件事,花在思考上的时间应该多于执行. 首先根据产品需求和设计图,脑中先建立一个产品架构: 1. 产品的定位是什么. 社交?媒体?游戏? ...

  9. Python——使用代码平台进行识别验证码

    打码平台介绍 一般使用超级鹰或打码兔的打码平台. 超级鹰介绍 打开http://www.chaojiying.com/contact.html注册用户,生成软件ID 下载python的demo文件 查 ...

  10. Python进程-理论

    进程定义 程序: 计算机程序是存储在磁盘上的可执行二进制(或其他类型)文件.只有把它们加载到内存中,并被操作系统调用,它们才会拥有其自己的生命周期. 进程: 进程则是表示的一个正在执行的程序.每个进程 ...