您好,登錄后才能下訂單哦!
本篇內容主要講解“dubbo的DubboSwaggerService有什么作用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“dubbo的DubboSwaggerService有什么作用”吧!
本文主要研究一下dubbo的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方法
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里頭添加了跨域設置
到此,相信大家對“dubbo的DubboSwaggerService有什么作用”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。