This is an automated email from the ASF dual-hosted git repository. wangxin pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git
The following commit(s) were added to refs/heads/master by this push: new 71fda0a Add Swagger UI integration and fixed #1642 (#1856) 71fda0a is described below commit 71fda0a5fd305d923579161d6c4cdd972f187033 Author: kimmking <kimmking...@gmail.com> AuthorDate: Wed Jun 6 16:27:13 2018 +0800 Add Swagger UI integration and fixed #1642 (#1856) * update cxf version and add test cases * support jdk7 * add profile for dependency in jdk9 * modify profile location * fix jaxb version * add dependency for jdk9 * extract dependencies to dependencies bom project * add SwaggerUI Integration for Rest web service * add approved license. * remove author info * adjust dependency order * add javadoc * exclude jsr311 1.1.1 version in pom * add test cases for Swagger Integration --- dependencies-bom/pom.xml | 14 +++++ dubbo-rpc/dubbo-rpc-rest/pom.xml | 51 +++++++++-------- .../swagger/DubboSwaggerApiListingResource.java | 47 ++++++++++++++++ .../integration/swagger/DubboSwaggerService.java | 43 +++++++++++++++ .../DubboSwaggerApiListingResourceTest.java | 64 ++++++++++++++++++++++ .../rest/integration/swagger/SwaggerService.java | 34 ++++++++++++ 6 files changed, 230 insertions(+), 23 deletions(-) diff --git a/dependencies-bom/pom.xml b/dependencies-bom/pom.xml index ac111df..9df4853 100644 --- a/dependencies-bom/pom.xml +++ b/dependencies-bom/pom.xml @@ -111,6 +111,8 @@ <jaxb_version>2.2.7</jaxb_version> <activation_version>1.2.0</activation_version> + + <swagger_version>1.5.19</swagger_version> </properties> <dependencyManagement> @@ -353,6 +355,18 @@ <version>${activation_version}</version> </dependency> + <!-- swagger --> + <dependency> + <groupId>io.swagger</groupId> + <artifactId>swagger-annotations</artifactId> + <version>${swagger_version}</version> + </dependency> + <dependency> + <groupId>io.swagger</groupId> + <artifactId>swagger-jaxrs</artifactId> + <version>${swagger_version}</version> + </dependency> + <!-- Test lib --> <dependency> <groupId>org.apache.curator</groupId> diff --git a/dubbo-rpc/dubbo-rpc-rest/pom.xml b/dubbo-rpc/dubbo-rpc-rest/pom.xml index 27f7c2b..eb9ab76 100644 --- a/dubbo-rpc/dubbo-rpc-rest/pom.xml +++ b/dubbo-rpc/dubbo-rpc-rest/pom.xml @@ -28,12 +28,16 @@ <skip_maven_deploy>false</skip_maven_deploy> </properties> <dependencies> + <dependency> + <groupId>com.alibaba</groupId> + <artifactId>dubbo-config-api</artifactId> + <version>${project.parent.version}</version> + </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo-rpc-api</artifactId> <version>${project.parent.version}</version> </dependency> - <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo-remoting-http</artifactId> @@ -67,28 +71,6 @@ <artifactId>resteasy-jdk-http</artifactId> </dependency> - <!--<dependency>--> - <!--<groupId>org.jboss.resteasy</groupId>--> - <!--<artifactId>tjws</artifactId>--> - <!--<version>3.0.7.Final</version>--> - <!--</dependency>--> - - <!--<dependency>--> - <!--<groupId>org.jboss.resteasy</groupId>--> - <!--<artifactId>resteasy-undertow</artifactId>--> - <!--<version>3.0.7.Final</version>--> - <!--</dependency>--> - <!--<dependency>--> - <!--<groupId>io.undertow</groupId>--> - <!--<artifactId>undertow-servlet</artifactId>--> - <!--<version>1.0.1.Final</version>--> - <!--</dependency>--> - <!--<dependency>--> - <!--<groupId>io.undertow</groupId>--> - <!--<artifactId>undertow-core</artifactId>--> - <!--<version>1.0.1.Final</version>--> - <!--</dependency>--> - <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jackson-provider</artifactId> @@ -104,11 +86,34 @@ <artifactId>netty-all</artifactId> </dependency> + <!-- swagger --> + <dependency> + <groupId>io.swagger</groupId> + <artifactId>swagger-annotations</artifactId> + <exclusions> + <exclusion> + <groupId>javax.ws.rs</groupId> + <artifactId>jsr311-api</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>io.swagger</groupId> + <artifactId>swagger-jaxrs</artifactId> + <exclusions> + <exclusion> + <groupId>javax.ws.rs</groupId> + <artifactId>jsr311-api</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo-serialization-jdk</artifactId> <version>${project.parent.version}</version> <scope>test</scope> </dependency> + </dependencies> </project> \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/com/alibaba/dubbo/rpc/protocol/rest/integration/swagger/DubboSwaggerApiListingResource.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/com/alibaba/dubbo/rpc/protocol/rest/integration/swagger/DubboSwaggerApiListingResource.java new file mode 100644 index 0000000..fc5ec6d --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/com/alibaba/dubbo/rpc/protocol/rest/integration/swagger/DubboSwaggerApiListingResource.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.dubbo.rpc.protocol.rest.integration.swagger; + +import com.alibaba.dubbo.config.annotation.Service; +import com.fasterxml.jackson.core.JsonProcessingException; +import io.swagger.jaxrs.listing.BaseApiListingResource; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletContext; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.UriInfo; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Application; + +@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; + } +} \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/com/alibaba/dubbo/rpc/protocol/rest/integration/swagger/DubboSwaggerService.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/com/alibaba/dubbo/rpc/protocol/rest/integration/swagger/DubboSwaggerService.java new file mode 100644 index 0000000..a692bf8 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/com/alibaba/dubbo/rpc/protocol/rest/integration/swagger/DubboSwaggerService.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.dubbo.rpc.protocol.rest.integration.swagger; + +import com.fasterxml.jackson.core.JsonProcessingException; + +import javax.servlet.ServletConfig; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.UriInfo; + + +@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; +} \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/com/alibaba/dubbo/rpc/protocol/rest/integration/swagger/DubboSwaggerApiListingResourceTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/com/alibaba/dubbo/rpc/protocol/rest/integration/swagger/DubboSwaggerApiListingResourceTest.java new file mode 100644 index 0000000..6af41c3 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/com/alibaba/dubbo/rpc/protocol/rest/integration/swagger/DubboSwaggerApiListingResourceTest.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.dubbo.rpc.protocol.rest.integration.swagger; + +import io.swagger.models.Swagger; +import org.jboss.resteasy.spi.ResteasyUriInfo; +import org.junit.Assert; +import org.junit.Test; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletContext; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.HashSet; +import java.util.Set; + +import static org.mockito.Mockito.*; + +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"))); + + Assert.assertNotNull(response); + Swagger swagger = (Swagger)response.getEntity(); + Assert.assertEquals("SwaggerService",swagger.getTags().get(0).getName()); + Assert.assertEquals("/demoService/hello",swagger.getPaths().keySet().toArray()[0].toString()); + } + +} diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/com/alibaba/dubbo/rpc/protocol/rest/integration/swagger/SwaggerService.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/com/alibaba/dubbo/rpc/protocol/rest/integration/swagger/SwaggerService.java new file mode 100644 index 0000000..59b5666 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/com/alibaba/dubbo/rpc/protocol/rest/integration/swagger/SwaggerService.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.dubbo.rpc.protocol.rest.integration.swagger; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.QueryParam; + +@Path("/demoService") +@Api(value = "SwaggerService") +public interface SwaggerService { + @GET + @Path("/hello") + @ApiOperation(value = "hello") + Integer hello(@QueryParam("a") Integer a, @QueryParam("b") Integer b); + +} -- To stop receiving notification emails like this one, please contact wang...@apache.org.