swagger结合dubbo的rest服务测试
背景介绍
我们应用的dubbo服务导出,可能没有直接的触发点去发起调用测试,除非自己手写controller和test类,缺乏一个动态工具,类似流行的swagger结合controller的测试页面,而swagger-dubbo就可以满足这个自动化测试场景需求。
准备知识
dubbo、swagger、spring
配置
-
web.xml配置springmvc的DispatcherServlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0" metadata-complete="true"> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:application/*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app> -
example-servlet.xml配置springmvc组件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <mvc:annotation-driven>
<!-- 支持fastjson -->
<!-- <mvc:message-converters>
<bean
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters> -->
</mvc:annotation-driven>
<context:annotation-config />
<context:component-scan base-package="com.deepoove.swagger.dubbo.example" />
<context:property-placeholder /> <!-- <context:property-placeholder location="classpath*:swagger-dubbo.properties" /> --> <bean class="com.deepoove.swagger.dubbo.spring.SwaggerDubboConfig" />
<bean class="com.deepoove.swagger.dubbo.example.AnnotationScanConfig" /> <mvc:resources location="/dist/" mapping="/dist/**" />
<mvc:resources location="/distv2/" mapping="/distv2/**" /> <!-- 跨域支持 -->
<mvc:cors>
<mvc:mapping path="/swagger-dubbo/**" allowed-origins="*" />
<mvc:mapping path="/h/**" allowed-origins="*" />
</mvc:cors> </beans> -
工程jar依赖
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>swagger-dubbo</artifactId>
<version>2.0.3</version>
</dependency>
使用
启动服务,访问链接http://127.0.0.1:8080/distv2/index.html,出现swagger的页面,并且输入配置json地址http://127.0.0.1:8080/swagger-dubbo/api-docs,查看显示的接口应该是你服务导出的所有dubbo接口
默认dubbo接口或实现类不加任何swagger的注解(比如Api,ApiParam等),则只取到类的基本信息,包括类名、方法、参数名称,所以信息需要自己添加swagger的注解到接口方法上面
选择一个接口测试,基本参数直接输入,复杂对象输入json串即可,如有访问不通,基本是跨域问题,配置host即可
基本原理
- com.deepoove.swagger.dubbo.spring.SwaggerDubboConfig配置类
- org.springframework.context.annotation.ConfigurationClassPostProcessor注解配置bean解析类
- com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan,dubbo扫包配置注解
- com.alibaba.dubbo.config.spring.extension.SpringExtensionFactory,dubbo spring扩展工厂
代码参考
- https://github.com/yaojf/swagger-dubbo
- 相对于fork的源码主要改动点,针对spring里面的动态代理类,获取动态代理类本身,获取具体的参数名称,并提供统一的配置bean
- 增加swagger.dubbo.open配置参数,对是否开启swagger-dubbo最开关,默认关闭