dubbo的DubboSwaggerService有什么作用
本篇内容主要讲解“dubbo的DubboSwaggerService有什么作用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“dubbo的DubboSwaggerService有什么作用”吧!
成都创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于网站设计制作、网站设计、新华网络推广、微信小程序开发、新华网络营销、新华企业策划、新华品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联公司为所有大学生创业者提供新华建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com
序
本文主要研究一下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> sets = new HashSet >(); 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里头添加了跨域设置
到此,相信大家对“dubbo的DubboSwaggerService有什么作用”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
文章标题:dubbo的DubboSwaggerService有什么作用
分享路径:http://myzitong.com/article/pipshs.html