swagger結合dubbo的rest服務測試

swagger結合dubbo的rest服務測試

背景介紹

咱們應用的dubbo服務導出,可能沒有直接的觸發點去發起調用測試,除非本身手寫controller和test類,缺少一個動態工具,相似流行的swagger結合controller的測試頁面,而swagger-dubbo就能夠知足這個自動化測試場景需求。html

準備知識

dubbo、swagger、springjava

配置

  1. web.xml配置springmvc的DispatcherServletgit

    <?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組件github

    <?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依賴web

    <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接口spring

  2. 默認dubbo接口或實現類不加任何swagger的註解(好比Api,ApiParam等),則只取到類的基本信息,包括類名、方法、參數名稱,因此信息須要本身添加swagger的註解到接口方法上面json

  3. 選擇一個接口測試,基本參數直接輸入,複雜對象輸入json串便可,若有訪問不通,基本是跨域問題,配置host便可api

基本原理

  • 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最開關,默認關閉
相關文章
相關標籤/搜索