From 11bf02b98b08d6ccab21fd8a89238d373753dfc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E4=BA=AE?= Date: Sun, 8 Dec 2024 22:54:16 +0800 Subject: [PATCH] add dubbo-triple-rest-swagger samples --- .../case-configuration.yml | 48 +++++++++++ .../case-versions.conf | 23 +++++ .../dubbo-samples-triple-rest-swagger/pom.xml | 67 +++++++++++++++ .../dubbo/rest/swagger/HelloService.java | 82 ++++++++++++++++++ .../dubbo/rest/swagger/HelloServiceImpl.java | 47 +++++++++++ .../rest/swagger/SwaggerApplication.java | 33 ++++++++ .../org/apache/dubbo/rest/swagger/User.java | 84 +++++++++++++++++++ .../src/main/resources/application.yml | 35 ++++++++ .../src/main/resources/log4j2.xml | 32 +++++++ .../dubbo/rest/swagger/test/ConsumerIT.java | 57 +++++++++++++ 2-advanced/dubbo-samples-triple-rest/pom.xml | 1 + 11 files changed, 509 insertions(+) create mode 100644 2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/case-configuration.yml create mode 100644 2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/case-versions.conf create mode 100644 2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/pom.xml create mode 100644 2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/java/org/apache/dubbo/rest/swagger/HelloService.java create mode 100644 2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/java/org/apache/dubbo/rest/swagger/HelloServiceImpl.java create mode 100644 2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/java/org/apache/dubbo/rest/swagger/SwaggerApplication.java create mode 100644 2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/java/org/apache/dubbo/rest/swagger/User.java create mode 100644 2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/resources/application.yml create mode 100644 2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/resources/log4j2.xml create mode 100644 2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/test/java/org/apache/dubbo/rest/swagger/test/ConsumerIT.java diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/case-configuration.yml b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/case-configuration.yml new file mode 100644 index 0000000000..1938bac080 --- /dev/null +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/case-configuration.yml @@ -0,0 +1,48 @@ +# 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. + +services: + zookeeper: + image: zookeeper:latest + + provider: + type: app + basedir: . + mainClass: org.apache.dubbo.rest.demo.SwaggerApplication + systemProps: + - zookeeper.address=zookeeper + - zookeeper.port=2181 + - dubbo.port=50052 + waitPortsBeforeRun: + - zookeeper:2181 + checkPorts: + - 50052 + checkLog: "dubbo service started" + depends_on: + - zookeeper + - + test: + type: test + basedir: . + tests: + - "**/*IT.class" + systemProps: + - dubbo.address=provider + waitPortsBeforeRun: + - provider:50052 + depends_on: + - provider + diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/case-versions.conf b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/case-versions.conf new file mode 100644 index 0000000000..f2f919d31d --- /dev/null +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/case-versions.conf @@ -0,0 +1,23 @@ +# +# 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. +# + +# Supported component versions of the test case + +# Spring app +dubbo.version=3.3.* +spring.version=6.* +java.version= [>= 17] diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/pom.xml b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/pom.xml new file mode 100644 index 0000000000..d654c3bbb3 --- /dev/null +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/pom.xml @@ -0,0 +1,67 @@ + + + 4.0.0 + + org.apache.dubbo + dubbo-samples-triple-rest + 1.0.0-SNAPSHOT + + + dubbo-samples-triple-rest-swagger + + + UTF-8 + 3.3.3-SNAPSHOT + + + + + org.springframework.boot + spring-boot-starter + + + org.apache.dubbo + dubbo-spring-boot-starter + ${dubbo.version} + + + org.springframework.boot + spring-boot-starter-log4j2 + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework + spring-web + 6.1.5 + test + + + + org.apache.dubbo + dubbo-rest-swagger + ${dubbo.version} + + + + org.apache.dubbo + dubbo-zookeeper-curator5-spring-boot-starter + ${dubbo.version} + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/java/org/apache/dubbo/rest/swagger/HelloService.java b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/java/org/apache/dubbo/rest/swagger/HelloService.java new file mode 100644 index 0000000000..f1720130c9 --- /dev/null +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/java/org/apache/dubbo/rest/swagger/HelloService.java @@ -0,0 +1,82 @@ +/* + * 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 org.apache.dubbo.rest.swagger; + +import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; + +import io.swagger.v3.oas.annotations.OpenAPIDefinition; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.info.Contact; +import io.swagger.v3.oas.annotations.info.Info; +import io.swagger.v3.oas.annotations.info.License; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.tags.Tag; + +@OpenAPIDefinition( + info = @Info( + title = "Dubbo Greeting API", + version = "1.0.0", + description = "This API provides greeting and health-check services.", + contact = @Contact( + name = "Dubbo Support", + email = "support@example.com", + url = "https://github.com/apache/dubbo/" + + ), + license = @License( + name = "Apache 2.0", + url = "https://www.apache.org/licenses/LICENSE-2.0.html" + ) + ), + tags = { + @Tag(name = "HelloService", description = "Provides greeting and health-check operations.") + } +) +@EnableDubbo +public interface HelloService { + /** + * Returns a personalized greeting message based on the user's details. + * + * @param user The user object containing details. + * @return A greeting message. + */ + @Operation(summary = "Get personalized greeting", description = "Returns a greeting message based on the user's details") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "Successfully returned greeting"), + @ApiResponse(responseCode = "400", description = "Invalid user data provided"), + @ApiResponse(responseCode = "500", description = "Internal server error") + }) + String sayHello( + @Parameter(description = "The user object containing user details", required = true) + User user + ); + + /** + * Checks the health status of the service. + * + * @return Health status of the service. + */ + @Operation(summary = "Health Check", description = "Checks if the service is running properly.") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "Service is healthy"), + @ApiResponse(responseCode = "500", description = "Service is down") + }) + String healthCheck(); +} diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/java/org/apache/dubbo/rest/swagger/HelloServiceImpl.java b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/java/org/apache/dubbo/rest/swagger/HelloServiceImpl.java new file mode 100644 index 0000000000..fac06bbf94 --- /dev/null +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/java/org/apache/dubbo/rest/swagger/HelloServiceImpl.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 org.apache.dubbo.rest.swagger; + +import org.apache.dubbo.config.annotation.DubboService; + +import io.swagger.v3.oas.annotations.Hidden; + +/** + * Implementation of HelloService interface. + */ +@DubboService +public class HelloServiceImpl implements HelloService { + + @Override + public String sayHello(User user) { + return "Hello, " + user.getName() + "! You are " + user.getAge() + " years old and enjoy " + + String.join(", ", user.getHobbies()) + "."; + } + + @Override + public String healthCheck() { + return "Service is running fine."; + } + + /** + * A hidden method not exposed in API documentation. + */ + @Hidden + public void hiddenMethod() { + System.out.println("This method is hidden and won't appear in the API documentation."); + } +} diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/java/org/apache/dubbo/rest/swagger/SwaggerApplication.java b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/java/org/apache/dubbo/rest/swagger/SwaggerApplication.java new file mode 100644 index 0000000000..cdce5aa91c --- /dev/null +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/java/org/apache/dubbo/rest/swagger/SwaggerApplication.java @@ -0,0 +1,33 @@ +/* + * 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 org.apache.dubbo.rest.swagger; + +import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +@EnableDubbo +public class SwaggerApplication { + + public static void main(String[] args) { + SpringApplication.run(SwaggerApplication.class, args); + System.out.println("dubbo service started"); + } +} diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/java/org/apache/dubbo/rest/swagger/User.java b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/java/org/apache/dubbo/rest/swagger/User.java new file mode 100644 index 0000000000..f57e8d7104 --- /dev/null +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/java/org/apache/dubbo/rest/swagger/User.java @@ -0,0 +1,84 @@ +/* + * 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 org.apache.dubbo.rest.swagger; + +import java.util.List; + +import io.swagger.v3.oas.annotations.media.Schema; + +/** + * Represents a user with details for demonstration purposes. + */ +@Schema(description = "Represents a user with name, age, and hobbies.") +public class User { + + @Schema(description = "Unique identifier of the user", example = "123") + private Long id; + + @Schema(description = "Name of the user", example = "Liang He") + private String name; + + @Schema(description = "Age of the user", example = "19") + private Integer age; + + @Schema(description = "List of user's hobbies", example = "[\"reading\", \"traveling\"]") + private List hobbies; + + public User() {} + + public User(Long id, String name, Integer age, List hobbies) { + this.id = id; + this.name = name; + this.age = age; + this.hobbies = hobbies; + } + + // Getters and Setters + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public List getHobbies() { + return hobbies; + } + + public void setHobbies(List hobbies) { + this.hobbies = hobbies; + } +} diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/resources/application.yml b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/resources/application.yml new file mode 100644 index 0000000000..34a20ae1a3 --- /dev/null +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/resources/application.yml @@ -0,0 +1,35 @@ +# 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. + +spring: + application: + name: dubbo-springboot-triple-rest-swagger +dubbo: + application: + name: ${spring.application.name} + qos-enable: false + protocol: + name: tri + port: 50052 + triple: + rest: + openapi: + enabled: true + registry: + id: zk-registry + address: zookeeper://127.0.0.1:2181 +server: + port: 8082 diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/resources/log4j2.xml b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/resources/log4j2.xml new file mode 100644 index 0000000000..f21b6cb1c5 --- /dev/null +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/main/resources/log4j2.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/test/java/org/apache/dubbo/rest/swagger/test/ConsumerIT.java b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/test/java/org/apache/dubbo/rest/swagger/test/ConsumerIT.java new file mode 100644 index 0000000000..88006252de --- /dev/null +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-swagger/src/test/java/org/apache/dubbo/rest/swagger/test/ConsumerIT.java @@ -0,0 +1,57 @@ +/* + * 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 org.apache.dubbo.rest.swagger.test; + +import org.apache.dubbo.config.annotation.DubboReference; +import org.junit.Assert; +import org.junit.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.web.client.RestClient; + +@SpringBootTest +public class ConsumerIT { + + private static final String HOST = System.getProperty("dubbo.address", "localhost"); + private final RestClient webClient = RestClient.create(); + + @DubboReference(url = "tri://${dubbo.address:localhost}:50052") + private static String toUri(String path) { + return "http://" + HOST + ":50052/" + path; + } + + @Test + public void testOpenApiDocumentation() { + // Request OpenAPI documentation + String result = webClient.get() + .uri(toUri("dubbo/openapi/api-docs")) + .retrieve() + .body(String.class); + + // Assert response is not null + Assert.assertNotNull("OpenAPI documentation response should not be null", result); + + // Validate expected endpoints in OpenAPI + assertContains(result, "/org.apache.dubbo.rest.swagger.HelloService/healthCheck"); + assertContains(result, "/org.apache.dubbo.rest.swagger.HelloService/sayHello"); + } + + // Utility method to assert a substring exists in a string + private void assertContains(String haystack, String needle) { + Assert.assertTrue("Expected substring not found: " + needle, haystack.contains(needle)); + } +} diff --git a/2-advanced/dubbo-samples-triple-rest/pom.xml b/2-advanced/dubbo-samples-triple-rest/pom.xml index d0910334d8..44b2e7f59e 100644 --- a/2-advanced/dubbo-samples-triple-rest/pom.xml +++ b/2-advanced/dubbo-samples-triple-rest/pom.xml @@ -47,6 +47,7 @@ dubbo-samples-triple-rest-jaxrs dubbo-samples-triple-rest-springmvc dubbo-samples-triple-rest-spring-security + dubbo-samples-triple-rest-swagger