专栏首页码匠的流水账聊聊dubbo的DubboSwaggerService

聊聊dubbo的DubboSwaggerService

本文主要研究一下dubbo的DubboSwaggerService

DubboSwaggerService

dubbo-2.7.2/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/integration/swagger/DubboSwaggerService.java

@Path("dubbo")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8", MediaType.TEXT_XML + "; " + "charset=UTF-8"})
public interface DubboSwaggerService {

    @GET
    @Path("swagger")
    public Response getListingJson(@Context Application app, @Context ServletConfig sc,
                                   @Context HttpHeaders headers, @Context UriInfo uriInfo) throws JsonProcessingException;
}
  • DubboSwaggerService定义了getListingJson方法

DubboSwaggerApiListingResource

dubbo-2.7.2/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/integration/swagger/DubboSwaggerApiListingResource.java

@Service
public class DubboSwaggerApiListingResource extends BaseApiListingResource implements DubboSwaggerService {

    @Context
    ServletContext context;

    @Override
    public Response getListingJson(Application app, ServletConfig sc,
                                   HttpHeaders headers, UriInfo uriInfo)  throws JsonProcessingException {
        Response response =  getListingJsonResponse(app, context, sc, headers, uriInfo);
        response.getHeaders().add("Access-Control-Allow-Origin", "*");
        response.getHeaders().add("Access-Control-Allow-Headers", "x-requested-with, ssi-token");
        response.getHeaders().add("Access-Control-Max-Age", "3600");
        response.getHeaders().add("Access-Control-Allow-Methods","GET,POST,PUT,DELETE,OPTIONS");
        return response;
    }
}
  • DubboSwaggerApiListingResource继承了swagger-jaxrs的BaseApiListingResource,同时实现了DubboSwaggerService接口;其实现的getListingJson方法首先调用父类的getListingJsonResponse获取response,然后往header里头添加了跨域设置

实例

dubbo-2.7.2/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/integration/swagger/DubboSwaggerApiListingResourceTest.java

public class DubboSwaggerApiListingResourceTest {

    private Application app;
    private ServletConfig sc;

    @Test
    public void test() throws Exception {

        DubboSwaggerApiListingResource resource = new  DubboSwaggerApiListingResource();

        app = mock(Application.class);
        sc = mock(ServletConfig.class);
        Set<Class<?>> sets = new HashSet<Class<?>>();
        sets.add(SwaggerService.class);

        when(sc.getServletContext()).thenReturn(mock(ServletContext.class));
        when(app.getClasses()).thenReturn(sets);

        Response response = resource.getListingJson(app, sc,
                null, new ResteasyUriInfo(new URI("http://rest.test")));

        Assertions.assertNotNull(response);
        Swagger swagger = (Swagger)response.getEntity();
        Assertions.assertEquals("SwaggerService",swagger.getTags().get(0).getName());
        Assertions.assertEquals("/demoService/hello",swagger.getPaths().keySet().toArray()[0].toString());
    }

}
  • 这里验证resource.getListingJson返回的entity的swagger的path

小结

DubboSwaggerService定义了getListingJson方法;DubboSwaggerApiListingResource继承了swagger-jaxrs的BaseApiListingResource,同时实现了DubboSwaggerService接口;其实现的getListingJson方法首先调用父类的getListingJsonResponse获取response,然后往header里头添加了跨域设置

doc

  • DubboSwaggerService

本文分享自微信公众号 - 码匠的流水账(geek_luandun),作者:码匠乱炖

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2019-07-06

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 聊聊dubbo的DubboSwaggerService

    dubbo-2.7.2/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol...

    codecraft
  • 聊聊dubbo的SpringContainer

    dubbo-2.7.2/dubbo-container/dubbo-container-api/src/main/java/org/apache/dubbo/c...

    codecraft
  • 聊聊dubbo的SpringContainer

    dubbo-2.7.2/dubbo-container/dubbo-container-api/src/main/java/org/apache/dubbo/c...

    codecraft
  • 聊聊dubbo的DubboSwaggerService

    dubbo-2.7.2/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol...

    codecraft
  • saltstack安装 原

    安装1:http://www.ttlsa.com/saltstack/install-saltstack-on-linux/

    尚浩宇
  • 前端自定义搜索框实现

    项目全部代码在自己的开源项目:https://github.com/u014427391/myblog

    用户1208223
  • Spring Security笔记:自定义登录页

    以下内容参考了 http://www.mkyong.com/spring-security/spring-security-form-login-example...

    菩提树下的杨过
  • 《Kotlin从零到精通Android开发》资源下载和内容勘误

    下面是《Android Studio开发实战 从零基础到App上线》一书用到的工具和代码资源: 1、本书使用的Android Studio版本为3.0.1,K...

    用户4464237
  • bootstrap快速入门笔记(九)-响应式工具

    从 v3.2.0 版本起,形如 .visible-*-* 的类针对每种屏幕大小都有了三种变体,每个针对 CSS 中不同的 display 属性,列表如下:

    用户3055976
  • 4-SIII-Android数据固化之Xml的Dom解析和存储

    张风捷特烈

扫码关注云+社区

领取腾讯云代金券