zoukankan      html  css  js  c++  java
  • swagger结合dubbo的rest服务测试

    swagger结合dubbo的rest服务测试

    背景介绍

    我们应用的dubbo服务导出,可能没有直接的触发点去发起调用测试,除非自己手写controller和test类,缺乏一个动态工具,类似流行的swagger结合controller的测试页面,而swagger-dubbo就可以满足这个自动化测试场景需求。

    准备知识

    dubbo、swagger、spring

    配置

    1. 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>
      
    2. 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>
      
    3. 工程jar依赖

      <dependency>
       <groupId>com.deepoove</groupId>
       <artifactId>swagger-dubbo</artifactId>
       <version>2.0.3</version>
      </dependency>
      

    使用

    1. 启动服务,访问链接http://127.0.0.1:8080/distv2/index.html,出现swagger的页面,并且输入配置json地址http://127.0.0.1:8080/swagger-dubbo/api-docs,查看显示的接口应该是你服务导出的所有dubbo接口

    2. 默认dubbo接口或实现类不加任何swagger的注解(比如Api,ApiParam等),则只取到类的基本信息,包括类名、方法、参数名称,所以信息需要自己添加swagger的注解到接口方法上面

    3. 选择一个接口测试,基本参数直接输入,复杂对象输入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最开关,默认关闭
  • 相关阅读:
    Android 中 Fragment 的切换(解决 replace 的低效)
    Android 中 OkGo 的使用 (封装 OkHttp)
    fastjson 封装工具类
    给系统添加右键使用 IDEA 打开的功能
    发现了一个很好看的博客园主题
    AndroidStudio中如何创建指定布局的layout文件
    转载:十个前端UI优秀框架
    win10 添加 telnet 工具
    tomcat各版本与jdk及servlet各版本对应关系
    servlet和jsp的maven依赖
  • 原文地址:https://www.cnblogs.com/yaojf/p/10488520.html
Copyright © 2011-2022 走看看