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/29 20:21:49 UTC
[camel-spring-boot] branch master updated: [CAMEL-14454]add
camel-example-spring-boot-rest-openapi example
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 df3e14e [CAMEL-14454]add camel-example-spring-boot-rest-openapi example
df3e14e is described below
commit df3e14e796a9fca886f52f7159ca875934542cfb
Author: Freeman Fang <fr...@gmail.com>
AuthorDate: Wed Jan 29 15:21:35 2020 -0500
[CAMEL-14454]add camel-example-spring-boot-rest-openapi example
---
.../README.adoc | 86 ++++++++++++++
.../camel-example-spring-boot-rest-openapi/pom.xml | 126 +++++++++++++++++++++
.../camel/example/springboot/Application.java | 34 ++++++
.../camel/example/springboot/CamelRouter.java | 88 ++++++++++++++
.../org/apache/camel/example/springboot/User.java | 60 ++++++++++
.../camel/example/springboot/UserService.java | 50 ++++++++
.../camel/example/springboot/UserServiceImpl.java | 51 +++++++++
.../src/main/resources/application.properties | 41 +++++++
8 files changed, 536 insertions(+)
diff --git a/examples/camel-example-spring-boot-rest-openapi/README.adoc b/examples/camel-example-spring-boot-rest-openapi/README.adoc
new file mode 100644
index 0000000..b205177
--- /dev/null
+++ b/examples/camel-example-spring-boot-rest-openapi/README.adoc
@@ -0,0 +1,86 @@
+== Spring Boot Example with Camel REST DSL and OpenApi
+
+=== Introduction
+
+This example illustrates how to use https://projects.spring.io/spring-boot/[Spring Boot] with http://camel.apache.org[Camel]. It provides a simple REST service that is created with http://camel.apache.org/rest-dsl.html[Camel REST DSL] and documented with http://swagger.io[OpenApi].
+
+The project uses the `camel-spring-boot-starter` dependency, a Spring Boot starter dependency for Camel that simplifies the Maven configuration.
+
+The project also uses `camel-servlet` component as the HTTP transport component for Camel REST DSL.
+
+=== Build
+
+You can build this example using:
+
+ $ mvn package
+
+=== Run
+
+You can run this example using:
+
+ $ mvn spring-boot:run
+
+You should see the following output when the application is launched:
+
+[source,text]
+----
+...
+[INFO] --- spring-boot-maven-plugin:1.5.10.RELEASE:run (default-cli) @ camel-example-spring-boot-rest-openapi ---
+[...]
+ . ____ _ __ _ _
+ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
+( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
+ \\/ ___)| |_)| | | | | || (_| | ) ) ) )
+ ' |____| .__|_| |_|_| |_\__, | / / / /
+ =========|_|==============|___/=/_/_/_/
+ :: Spring Boot :: (v2.0.0.RELEASE)
+[...]
+2017-03-05 14:55:44.032 INFO 15312 --- [ main] o.a.camel.spring.SpringCamelContext : Total 4 routes, of which 4 are started.
+2017-03-05 14:55:44.034 INFO 15312 --- [ main] o.a.camel.spring.SpringCamelContext : Apache Camel 2.22.0-SNAPSHOT (CamelContext: camel-1) started in 0.614 seconds
+2017-03-05 14:55:44.131 INFO 15312 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
+2017-03-05 14:55:44.140 INFO 15312 --- [ main] o.a.c.example.springboot.Application : Started Application in 6.265 seconds (JVM running for 21.092)
+----
+
+After the Spring Boot application is started, you can open the following URL in your web browser to access the REST endpoint and retrieve a list of users: http://localhost:8080/camel/users
+
+You can also access the REST endpoint from the command line:
+
+[source,text]
+----
+$ curl http://localhost:8080/api/users
+----
+
+The command will produce the following output:
+
+[source,json]
+----
+[ {
+ "id" : 1,
+ "name" : "John Coltrane"
+}, {
+ "id" : 2,
+ "name" : "Miles Davis"
+}, {
+ "id" : 3,
+ "name" : "Sonny Rollins"
+} ]
+----
+
+The OpenApi documentation is located at: `\http://localhost:8080/api/api-doc` and can be retrieved with the following command:
+
+[source,text]
+----
+$ curl http://localhost:8080/api/api-doc
+----
+
+The Spring Boot application can be stopped pressing `[CTRL] + [C]` in the shell.
+
+=== 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/pom.xml b/examples/camel-example-spring-boot-rest-openapi/pom.xml
new file mode 100644
index 0000000..cbe5e40
--- /dev/null
+++ b/examples/camel-example-spring-boot-rest-openapi/pom.xml
@@ -0,0 +1,126 @@
+<!--
+
+ 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</artifactId>
+ <name>Camel SB Examples :: REST DSL and OpenApi</name>
+ <description>An example showing Camel REST DSL and OpenApi with Spring Boot</description>
+
+ <properties>
+ <category>Rest</category>
+ <spring.boot-version>${spring-boot-version}</spring.boot-version>
+ </properties>
+
+ <!-- Spring-Boot and Camel BOM -->
+ <dependencyManagement>
+ <dependencies>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-dependencies</artifactId>
+ <version>${spring.boot-version}</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
+ <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>
+ <!-- Spring Boot -->
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-web</artifactId>
+ <exclusions>
+ <exclusion>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-tomcat</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-undertow</artifactId>
+ </dependency>
+
+ <!-- Camel -->
+ <dependency>
+ <groupId>org.apache.camel.springboot</groupId>
+ <artifactId>camel-spring-boot-starter</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel.springboot</groupId>
+ <artifactId>camel-openapi-java-starter</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel.springboot</groupId>
+ <artifactId>camel-servlet-starter</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel.springboot</groupId>
+ <artifactId>camel-jackson-starter</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ </dependency>
+
+ <!-- Test -->
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-test</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-test</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <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/src/main/java/org/apache/camel/example/springboot/Application.java b/examples/camel-example-spring-boot-rest-openapi/src/main/java/org/apache/camel/example/springboot/Application.java
new file mode 100644
index 0000000..4c0bd71
--- /dev/null
+++ b/examples/camel-example-spring-boot-rest-openapi/src/main/java/org/apache/camel/example/springboot/Application.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 org.apache.camel.example.springboot;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+// CHECKSTYLE:OFF
+@SpringBootApplication
+public class Application {
+
+ /**
+ * Main method to start the application.
+ */
+ public static void main(String[] args) {
+ SpringApplication.run(Application.class, args);
+ }
+
+}
+// CHECKSTYLE:ON
diff --git a/examples/camel-example-spring-boot-rest-openapi/src/main/java/org/apache/camel/example/springboot/CamelRouter.java b/examples/camel-example-spring-boot-rest-openapi/src/main/java/org/apache/camel/example/springboot/CamelRouter.java
new file mode 100644
index 0000000..d139129
--- /dev/null
+++ b/examples/camel-example-spring-boot-rest-openapi/src/main/java/org/apache/camel/example/springboot/CamelRouter.java
@@ -0,0 +1,88 @@
+/*
+ * 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.springboot;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.rest.RestBindingMode;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.core.env.Environment;
+import org.springframework.stereotype.Component;
+
+import static org.apache.camel.model.rest.RestParamType.body;
+import static org.apache.camel.model.rest.RestParamType.path;
+
+/**
+ * A simple Camel REST DSL route with OpenApi API documentation.
+ */
+@Component
+public class CamelRouter extends RouteBuilder {
+
+ @Autowired
+ private Environment env;
+
+ @Value("${camel.component.servlet.mapping.context-path}")
+ private String contextPath;
+
+ @Override
+ public void configure() throws Exception {
+
+ // @formatter:off
+
+ // this can also be configured in application.properties
+ restConfiguration()
+ .component("servlet")
+ .bindingMode(RestBindingMode.json)
+ .dataFormatProperty("prettyPrint", "true")
+ .enableCORS(true)
+ .port(env.getProperty("server.port", "8080"))
+ .contextPath(contextPath.substring(0, contextPath.length() - 2))
+ // turn on openapi api-doc
+ .apiContextPath("/api-doc")
+ .apiProperty("api.title", "User API")
+ .apiProperty("api.version", "1.0.0");
+
+ rest("/users").description("User REST service")
+ .consumes("application/json")
+ .produces("application/json")
+
+ .get().description("Find all users").outType(User[].class)
+ .responseMessage().code(200).message("All users successfully returned").endResponseMessage()
+ .to("bean:userService?method=findUsers")
+
+ .get("/{id}").description("Find user by ID")
+ .outType(User.class)
+ .param().name("id").type(path).description("The ID of the user").dataType("integer").endParam()
+ .responseMessage().code(200).message("User successfully returned").endResponseMessage()
+ .to("bean:userService?method=findUser(${header.id})")
+
+ .put("/{id}").description("Update a user").type(User.class)
+ .param().name("id").type(path).description("The ID of the user to update").dataType("integer").endParam()
+ .param().name("body").type(body).description("The user to update").endParam()
+ .responseMessage().code(204).message("User successfully updated").endResponseMessage()
+ .to("direct:update-user");
+
+ from("direct:update-user")
+ .to("bean:userService?method=updateUser")
+ .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(204))
+ .setBody(constant(""));
+
+ // @formatter:on
+ }
+
+}
diff --git a/examples/camel-example-spring-boot-rest-openapi/src/main/java/org/apache/camel/example/springboot/User.java b/examples/camel-example-spring-boot-rest-openapi/src/main/java/org/apache/camel/example/springboot/User.java
new file mode 100644
index 0000000..7d953ca
--- /dev/null
+++ b/examples/camel-example-spring-boot-rest-openapi/src/main/java/org/apache/camel/example/springboot/User.java
@@ -0,0 +1,60 @@
+/*
+ * 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.springboot;
+
+import org.apache.commons.lang3.builder.ToStringBuilder;
+
+/**
+ * User entity
+ *
+ */
+public class User {
+
+ private Integer id;
+
+ private String name;
+
+ public User() {
+ }
+
+ public User(Integer id, String name) {
+ this.id = id;
+ this.name = name;
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public String toString() {
+ return ToStringBuilder.reflectionToString(this);
+ }
+
+}
diff --git a/examples/camel-example-spring-boot-rest-openapi/src/main/java/org/apache/camel/example/springboot/UserService.java b/examples/camel-example-spring-boot-rest-openapi/src/main/java/org/apache/camel/example/springboot/UserService.java
new file mode 100644
index 0000000..5208aab
--- /dev/null
+++ b/examples/camel-example-spring-boot-rest-openapi/src/main/java/org/apache/camel/example/springboot/UserService.java
@@ -0,0 +1,50 @@
+/*
+ * 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.springboot;
+
+import java.util.Collection;
+
+/**
+ * Service interface for managing users.
+ */
+public interface UserService {
+
+ /**
+ * Find a user by the given ID
+ *
+ * @param id
+ * the ID of the user
+ * @return the user, or <code>null</code> if user not found.
+ */
+ User findUser(Integer id);
+
+ /**
+ * Find all users
+ *
+ * @return a collection of all users
+ */
+ Collection<User> findUsers();
+
+ /**
+ * Update the given user
+ *
+ * @param user
+ * the user
+ */
+ void updateUser(User user);
+
+}
diff --git a/examples/camel-example-spring-boot-rest-openapi/src/main/java/org/apache/camel/example/springboot/UserServiceImpl.java b/examples/camel-example-spring-boot-rest-openapi/src/main/java/org/apache/camel/example/springboot/UserServiceImpl.java
new file mode 100644
index 0000000..69b6d60
--- /dev/null
+++ b/examples/camel-example-spring-boot-rest-openapi/src/main/java/org/apache/camel/example/springboot/UserServiceImpl.java
@@ -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.camel.example.springboot;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.TreeMap;
+
+import org.springframework.stereotype.Service;
+
+@Service("userService")
+public class UserServiceImpl implements UserService {
+
+ private final Map<Integer, User> users = new TreeMap<>();
+
+ public UserServiceImpl() {
+ users.put(1, new User(1, "John Coltrane"));
+ users.put(2, new User(2, "Miles Davis"));
+ users.put(3, new User(3, "Sonny Rollins"));
+ }
+
+ @Override
+ public User findUser(Integer id) {
+ return users.get(id);
+ }
+
+ @Override
+ public Collection<User> findUsers() {
+ return users.values();
+ }
+
+ @Override
+ public void updateUser(User user) {
+ users.put(user.getId(), user);
+ }
+
+}
diff --git a/examples/camel-example-spring-boot-rest-openapi/src/main/resources/application.properties b/examples/camel-example-spring-boot-rest-openapi/src/main/resources/application.properties
new file mode 100644
index 0000000..cfab0c7
--- /dev/null
+++ b/examples/camel-example-spring-boot-rest-openapi/src/main/resources/application.properties
@@ -0,0 +1,41 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+# the name of Camel
+camel.springboot.name = MyCamel
+
+# to reconfigure the camel servlet context-path mapping to use /api/* instead of /camel/*
+camel.component.servlet.mapping.context-path=/api/*
+
+# rest can also be configured here instead in the CamelRouter class
+# rest DSL configuration
+#camel.rest.component=servlet
+#camel.rest.binding-mode=json
+#camel.rest.data-format-property[prettyPrint]=true
+#camel.rest.enable-cors=true
+#camel.rest.port=${server.port:8080}
+#camel.rest.context-path=/api
+
+# rest DSL api-doc configuration
+#camel.rest.api-context-path=/api-doc
+#camel.rest.api-property[api.title]=User API
+#camel.rest.api-property[api.version]=1.0.0
+
+# to configure logging levels
+#logging.level.org.springframework = INFO
+#logging.level.org.apache.camel.spring.boot = INFO
+#logging.level.org.apache.camel.impl = DEBUG