You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ff...@apache.org on 2020/01/30 18:36:22 UTC

[camel-spring-boot] branch master updated: [CAMEL-14454]add camel-example-spring-boot-rest-openapi-simple

This is an automated email from the ASF dual-hosted git repository.

ffang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-spring-boot.git


The following commit(s) were added to refs/heads/master by this push:
     new 914bfbd  [CAMEL-14454]add camel-example-spring-boot-rest-openapi-simple
914bfbd is described below

commit 914bfbd3ed358941da4b2c46219e2ff0276e7c6a
Author: Freeman Fang <fr...@gmail.com>
AuthorDate: Thu Jan 30 13:35:53 2020 -0500

    [CAMEL-14454]add camel-example-spring-boot-rest-openapi-simple
---
 .../README.adoc                                    |  41 ++++++++
 .../pom.xml                                        | 104 +++++++++++++++++++++
 .../camel/example/RestOpenApiApplication.java      |  72 ++++++++++++++
 examples/pom.xml                                   |   1 +
 4 files changed, 218 insertions(+)

diff --git a/examples/camel-example-spring-boot-rest-openapi-simple/README.adoc b/examples/camel-example-spring-boot-rest-openapi-simple/README.adoc
new file mode 100644
index 0000000..d27ca3c
--- /dev/null
+++ b/examples/camel-example-spring-boot-rest-openapi-simple/README.adoc
@@ -0,0 +1,41 @@
+== Camel Rest OpenApi example
+
+This example shows how to call a REST service defined using OpenApi
+specification with the help of Camel Rest OpenApi component.
+
+The example is a standalong Spring Boot application that acts as a REST
+client, you can run simply by issuing:
+
+....
+$ mvn spring-boot:run
+....
+
+Or by packaging it and running it using `+java+` CLI:
+
+....
+$ mvn package
+$ java -jar target/camel-example-spring-boot-rest-openapi-simple-*.jar
+....
+
+The example by default uses the PetStore demo hosted on swagger.io and
+invokes the `+logoutUser+` operation. You can make it call any API
+that you have OpenApi specification for and any operation with simple
+arguments, for instance this retrives a pet from the PetStore demo with
+ID `+14+`:
+
+....
+$ java -jar target/camel-example-spring-boot-rest-openapi-*.jar \
+        --openapi=https://petstore3.swagger.io/api/v3/openapi.json \
+        --operation=getPetById \
+        --petId=14
+....
+
+=== Help and contributions
+
+If you hit any problem using Camel or have some feedback, then please
+https://camel.apache.org/support.html[let us know].
+
+We also love contributors, so
+https://camel.apache.org/contributing.html[get involved] :-)
+
+The Camel riders!
diff --git a/examples/camel-example-spring-boot-rest-openapi-simple/pom.xml b/examples/camel-example-spring-boot-rest-openapi-simple/pom.xml
new file mode 100644
index 0000000..b141427
--- /dev/null
+++ b/examples/camel-example-spring-boot-rest-openapi-simple/pom.xml
@@ -0,0 +1,104 @@
+<?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.
+
+-->
+<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.camel.springboot.example</groupId>
+        <artifactId>examples</artifactId>
+        <version>3.1.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>camel-example-spring-boot-rest-openapi-simple</artifactId>
+    <name>Camel SB Examples :: Rest :: OpenApi Simple</name>
+    <description>This example shows how to call a Rest service defined using OpenApi specification</description>
+
+    <properties>
+        <category>Beginner</category>
+        <title>REST OpenApi</title>
+
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+        <spring.boot-version>${spring-boot-version}</spring.boot-version>
+    </properties>
+
+    <dependencyManagement>
+        <dependencies>
+            <!-- Spring Boot BOM -->
+            <dependency>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-dependencies</artifactId>
+                <version>${spring.boot-version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+            <!-- Camel BOM -->
+            <dependency>
+                <groupId>org.apache.camel.springboot</groupId>
+                <artifactId>camel-spring-boot-dependencies</artifactId>
+                <version>${project.version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
+    <dependencies>
+
+        <dependency>
+            <groupId>org.apache.camel.springboot</groupId>
+            <artifactId>camel-http-starter</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.camel.springboot</groupId>
+            <artifactId>camel-rest-openapi-starter</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+    </dependencies>
+
+    <build>
+
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-resources-plugin</artifactId>
+                <version>${maven-resources-plugin-version}</version>
+                <configuration>
+                    <encoding>UTF-8</encoding>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+                <version>${spring-boot-version}</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>repackage</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>
diff --git a/examples/camel-example-spring-boot-rest-openapi-simple/src/main/java/org/apache/camel/example/RestOpenApiApplication.java b/examples/camel-example-spring-boot-rest-openapi-simple/src/main/java/org/apache/camel/example/RestOpenApiApplication.java
new file mode 100644
index 0000000..0582a2c
--- /dev/null
+++ b/examples/camel-example-spring-boot-rest-openapi-simple/src/main/java/org/apache/camel/example/RestOpenApiApplication.java
@@ -0,0 +1,72 @@
+/*
+ * 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.camel.example;
+
+import java.util.Map;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+import static java.util.function.Function.identity;
+
+import org.apache.camel.ProducerTemplate;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.ApplicationArguments;
+import org.springframework.boot.ApplicationRunner;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.ApplicationContext;
+
+@SpringBootApplication
+public class RestOpenApiApplication implements ApplicationRunner {
+
+    @Autowired
+    ApplicationContext context;
+
+    @Value("${operation:logoutUser}")
+    String operation;
+
+    @Value("${openapi:https://petstore3.swagger.io/api/v3/openapi.json}")
+    String specificationUri;
+
+    @Value("${host:https://petstore3.swagger.io}")
+    String host;
+
+    @Autowired
+    ProducerTemplate template;
+
+    @Override
+    public void run(final ApplicationArguments args) throws Exception {
+        final Predicate<String> operations = "operation"::equals;
+
+        final Map<String, Object> headers = args.getOptionNames().stream().filter(operations.negate())
+            .collect(Collectors.toMap(identity(), arg -> args.getOptionValues(arg).get(0)));
+
+        final String body = template.requestBodyAndHeaders("rest-openapi:" + specificationUri + "#" + operation + "?host=" + host, null,
+            headers, String.class);
+
+        System.out.println(body);
+
+        SpringApplication.exit(context, () -> 0);
+    }
+
+    public static void main(final String[] args) {
+        SpringApplication.run(RestOpenApiApplication.class, args);
+    }
+
+}
diff --git a/examples/pom.xml b/examples/pom.xml
index 3fa7080..d331a2b 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -63,6 +63,7 @@
         <module>camel-example-spring-boot-rest-swagger</module>
         <module>camel-example-spring-boot-rest-swagger-simple</module>
         <module>camel-example-spring-boot-rest-openapi</module>
+        <module>camel-example-spring-boot-rest-openapi-simple</module>
         <module>camel-example-spring-boot-servicecall</module>
         <module>camel-example-spring-boot-supervising-route-controller</module>
         <module>camel-example-spring-boot-twitter-salesforce</module>