Skip to content

Commit

Permalink
add dubbo-samples-openapi-advance
Browse files Browse the repository at this point in the history
  • Loading branch information
heliang666s committed Dec 30, 2024
1 parent 609d867 commit 3fa3c5a
Show file tree
Hide file tree
Showing 11 changed files with 357 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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.

services:
zookeeper:
image: zookeeper:latest

provider:
type: app
basedir: .
mainClass: org.apache.dubbo.rest.demo.OpenAPIApplication
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
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-samples-triple-rest</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>dubbo-samples-triple-rest-openapi</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<dubbo.version>3.3.3-SNAPSHOT</dubbo.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-zookeeper-curator5-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>6.1.5</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -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.
*/

package org.apache.dubbo.rest.openapi.advance;

import org.apache.dubbo.remoting.http12.HttpMethods;
import org.apache.dubbo.remoting.http12.rest.Mapping;
import org.apache.dubbo.remoting.http12.rest.Param;
import org.apache.dubbo.remoting.http12.rest.ParamType;

public interface DemoService {

String hello(String name);

@Mapping(path = "/hi", method = HttpMethods.POST)
String hello(User user, @Param(value = "c", type = ParamType.Header) int count);

@Mapping
String helloUser(User user);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.apache.dubbo.rest.openapi.advance;

import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.remoting.http12.rest.OpenAPI;
import org.apache.dubbo.remoting.http12.rest.Operation;
import org.apache.dubbo.remoting.http12.rest.Schema;

@DubboService
@OpenAPI(infoTitle = "Dubbo OpenAPI", infoDescription = "This API provides greeting services for users.", infoVersion = "v1", docDescription = "API for greeting users with their names and titles.")
public class DemoServiceImpl implements DemoService {

@Operation(description = "Returns a greeting message with the provided name.")
@Override
public String hello(@Schema(description = "Name of the person to greet") String name) {
return "Hello " + name;
}

@Operation(description = "Returns a greeting message with user's title and name, along with a count.")
@Override
public String hello(
@Schema(description = "User object containing title and name") User user,
@Schema(description = "Number of times to greet") int count) {
return "Hello " + user.getTitle() + ". " + user.getName() + ", " + count;
}

@Operation(description = "Returns a greeting message with user's title and name.")
@Override
public String helloUser(@Schema(description = "User object containing title and name") User user) {
return "Hello " + user.getTitle() + ". " + user.getName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.apache.dubbo.rest.openapi.advance;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class OpenAPIApplication {
public static void main(String[] args) {
SpringApplication.run(OpenAPIApplication.class, args);
System.out.println("dubbo service started");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.apache.dubbo.rest.openapi.advance;

public class User {
private String title;
private String name;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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-basic
dubbo:
application:
name: ${spring.application.name}
qos-enable: false
protocol:
name: tri
port: 50052
triple:
verbose: true
triple:
rest:
openapi:
enabled: true
registry:
id: zk-registry
address: zookeeper://127.0.0.1:2181
server:
port: 8082
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="%style{%d{HH:mm:ss.SSS}}{Magenta} %style{|-}{White}%highlight{%-5p} [%t] %style{%40.40c}{Cyan}:%style{%-3L}{Blue} %style{-|}{White} %m%n%rEx{filters(jdk.internal.reflect,java.lang.reflect,sun.reflect)}" disableAnsi="false" charset="UTF-8"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
<Logger name="org.apache.dubbo.rpc.protocol.tri" level="debug"/>
<Logger name="org.apache.dubbo.remoting.http12" level="debug"/>
<Logger name="org.apache.dubbo.config" level="warn"/>
</Loggers>
</Configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.openapi.advance.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 helloWithRest() {
String result = webClient.get().
uri(toUri("/dubbo/openapi/api-docs")).
retrieve().
body(String.class);
System.out.println(result);
Assert.assertNotNull("OpenAPI documentation response should not be null", result);
Assert.assertTrue(result.contains("/org.apache.dubbo.rest.demo.DemoService/hello"));
Assert.assertTrue(result.contains("/org.apache.dubbo.rest.demo.DemoService/helloUser"));
Assert.assertTrue(result.contains("/org.apache.dubbo.rest.demo.DemoService/hi"));
}
}
1 change: 1 addition & 0 deletions 2-advanced/dubbo-samples-triple-rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<module>dubbo-samples-triple-rest-jaxrs</module>
<module>dubbo-samples-triple-rest-springmvc</module>
<module>dubbo-samples-triple-rest-spring-security</module>
<module>dubbo-samples-triple-rest-openapi-advance</module>
</modules>

<dependencyManagement>
Expand Down

0 comments on commit 3fa3c5a

Please sign in to comment.