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最開關,默認關閉


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM