camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gzurow...@apache.org
Subject camel git commit: CAMEL-9800: Add Spring Boot example with REST DSL and Swagger
Date Sun, 05 Mar 2017 17:16:20 GMT
Repository: camel
Updated Branches:
  refs/heads/master abda88d2b -> 272b48aa9


CAMEL-9800: Add Spring Boot example with REST DSL and Swagger

Signed-off-by: Gregor Zurowski <gregor@zurowski.org>

Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/272b48aa
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/272b48aa
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/272b48aa

Branch: refs/heads/master
Commit: 272b48aa9129a670a123eed3fcc23d812ddfad04
Parents: abda88d
Author: Gregor Zurowski <gregor@zurowski.org>
Authored: Sun Mar 5 18:14:51 2017 +0100
Committer: Gregor Zurowski <gregor@zurowski.org>
Committed: Sun Mar 5 18:14:51 2017 +0100

----------------------------------------------------------------------
 .../README.adoc                                 |  76 +++++++++++++
 .../pom.xml                                     | 110 +++++++++++++++++++
 .../camel/example/springboot/Application.java   |  42 +++++++
 .../camel/example/springboot/CamelRouter.java   |  69 ++++++++++++
 .../apache/camel/example/springboot/User.java   |  66 +++++++++++
 .../camel/example/springboot/UserService.java   |  51 +++++++++
 .../example/springboot/UserServiceImpl.java     |  51 +++++++++
 7 files changed, 465 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/272b48aa/examples/camel-example-spring-boot-rest-swagger/README.adoc
----------------------------------------------------------------------
diff --git a/examples/camel-example-spring-boot-rest-swagger/README.adoc b/examples/camel-example-spring-boot-rest-swagger/README.adoc
new file mode 100644
index 0000000..8c0ed69
--- /dev/null
+++ b/examples/camel-example-spring-boot-rest-swagger/README.adoc
@@ -0,0 +1,76 @@
+# Spring Boot Example with Camel REST DSL and Swagger
+
+### 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[Swagger].
+
+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:
+
+----
+[...]
+[INFO] --- spring-boot-maven-plugin:1.5.1.RELEASE:run (default-cli) @ camel-example-spring-boot-rest-swagger
---
+  .   ____          _            __ _ _
+ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
+( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
+ \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
+  '  |____| .__|_| |_|_| |_\__, | / / / /
+ =========|_|==============|___/=/_/_/_/
+ :: Spring Boot ::        (v1.5.1.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.19.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:
+
+    $ curl http://localhost:8080/camel/users
+
+
+The command will produce the following output:
+
+----
+[ {
+  "id" : 1,
+  "name" : "John Coltrane"
+}, {
+  "id" : 2,
+  "name" : "Miles Davis"
+}, {
+  "id" : 3,
+  "name" : "Sonny Rollins"
+} ]
+----
+
+The Swagger documentation is located at: http://localhost:8080/camel/api-doc and can be retrieved
with the following command:
+
+    $ curl http://localhost:8080/camel/api-doc
+
+The Spring Boot application can be stopped pressing `[CTRL]`+`[C]` in the shell.
+
+### Help
+
+If you hit any problems please let us know on the http://camel.apache.org/discussion-forums.html[Camel
Forums].
+
+Please help us make Apache Camel better - we appreciate any feedback you may have. Enjoy!
+
+The Camel riders!

http://git-wip-us.apache.org/repos/asf/camel/blob/272b48aa/examples/camel-example-spring-boot-rest-swagger/pom.xml
----------------------------------------------------------------------
diff --git a/examples/camel-example-spring-boot-rest-swagger/pom.xml b/examples/camel-example-spring-boot-rest-swagger/pom.xml
new file mode 100644
index 0000000..77ba6f9
--- /dev/null
+++ b/examples/camel-example-spring-boot-rest-swagger/pom.xml
@@ -0,0 +1,110 @@
+<?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</groupId>
+    <artifactId>examples</artifactId>
+    <version>2.19.0-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>camel-example-spring-boot-rest-swagger</artifactId>
+  <name>Camel :: Example :: Spring Boot with REST DSL and Swagger</name>
+  <description>An example showing Camel REST DSL and Swagger with Spring Boot</description>
+
+  <properties>
+    <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</groupId>
+        <artifactId>camel-parent</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>
+    </dependency>
+
+    <!-- Camel -->
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-spring-boot-starter</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-swagger-java</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-servlet</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-jackson</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>

http://git-wip-us.apache.org/repos/asf/camel/blob/272b48aa/examples/camel-example-spring-boot-rest-swagger/src/main/java/org/apache/camel/example/springboot/Application.java
----------------------------------------------------------------------
diff --git a/examples/camel-example-spring-boot-rest-swagger/src/main/java/org/apache/camel/example/springboot/Application.java
b/examples/camel-example-spring-boot-rest-swagger/src/main/java/org/apache/camel/example/springboot/Application.java
new file mode 100644
index 0000000..9bbb662
--- /dev/null
+++ b/examples/camel-example-spring-boot-rest-swagger/src/main/java/org/apache/camel/example/springboot/Application.java
@@ -0,0 +1,42 @@
+/**
+ * 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.component.servlet.CamelHttpTransportServlet;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.web.servlet.ServletRegistrationBean;
+import org.springframework.context.annotation.Bean;
+
+@SpringBootApplication
+public class Application {
+
+    /**
+     * Main method to start the application.
+     */
+    public static void main(String[] args) {
+        SpringApplication.run(Application.class, args);
+    }
+
+    @Bean
+    public ServletRegistrationBean camelServletRegistrationBean() {
+        ServletRegistrationBean registration = new ServletRegistrationBean(new CamelHttpTransportServlet(),
"/camel/*");
+        registration.setName("CamelServlet");
+        return registration;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/272b48aa/examples/camel-example-spring-boot-rest-swagger/src/main/java/org/apache/camel/example/springboot/CamelRouter.java
----------------------------------------------------------------------
diff --git a/examples/camel-example-spring-boot-rest-swagger/src/main/java/org/apache/camel/example/springboot/CamelRouter.java
b/examples/camel-example-spring-boot-rest-swagger/src/main/java/org/apache/camel/example/springboot/CamelRouter.java
new file mode 100644
index 0000000..b1a7a28
--- /dev/null
+++ b/examples/camel-example-spring-boot-rest-swagger/src/main/java/org/apache/camel/example/springboot/CamelRouter.java
@@ -0,0 +1,69 @@
+/**
+ * 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 static org.apache.camel.model.rest.RestParamType.body;
+import static org.apache.camel.model.rest.RestParamType.path;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.rest.RestBindingMode;
+import org.springframework.stereotype.Component;
+
+/**
+ * A simple Camel REST DSL route with Swagger API documentation.
+ * 
+ */
+@Component
+public class CamelRouter extends RouteBuilder {
+
+    @Override
+    public void configure() throws Exception {
+
+        // @formatter:off
+        restConfiguration()
+            .component("servlet")
+            .bindingMode(RestBindingMode.json)
+            .dataFormatProperty("prettyPrint", "true")
+            .apiContextPath("/api-doc")
+                .apiProperty("api.title", "User API").apiProperty("api.version", "1.0.0")
+                .apiProperty("cors", "true");
+
+        
+        rest("/users").description("User REST service")
+            .consumes("application/json")
+            .produces("application/json")
+
+            .get().description("Find all users").outTypeList(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("bean:userService?method=updateUser");
+
+        // @formatter:on
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/272b48aa/examples/camel-example-spring-boot-rest-swagger/src/main/java/org/apache/camel/example/springboot/User.java
----------------------------------------------------------------------
diff --git a/examples/camel-example-spring-boot-rest-swagger/src/main/java/org/apache/camel/example/springboot/User.java
b/examples/camel-example-spring-boot-rest-swagger/src/main/java/org/apache/camel/example/springboot/User.java
new file mode 100644
index 0000000..8ea297e
--- /dev/null
+++ b/examples/camel-example-spring-boot-rest-swagger/src/main/java/org/apache/camel/example/springboot/User.java
@@ -0,0 +1,66 @@
+/**
+ * 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;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+
+/**
+ * User entity
+ *
+ */
+@ApiModel(description = "Represents an user of the system")
+public class User {
+
+    @ApiModelProperty(value = "The ID of the user", required = true)
+    private Integer id;
+
+    @ApiModelProperty(value = "The name of the user", required = true)
+    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);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/272b48aa/examples/camel-example-spring-boot-rest-swagger/src/main/java/org/apache/camel/example/springboot/UserService.java
----------------------------------------------------------------------
diff --git a/examples/camel-example-spring-boot-rest-swagger/src/main/java/org/apache/camel/example/springboot/UserService.java
b/examples/camel-example-spring-boot-rest-swagger/src/main/java/org/apache/camel/example/springboot/UserService.java
new file mode 100644
index 0000000..072a672
--- /dev/null
+++ b/examples/camel-example-spring-boot-rest-swagger/src/main/java/org/apache/camel/example/springboot/UserService.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;
+
+/**
+ * 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.
+     */
+    public User findUser(Integer id);
+
+    /**
+     * Find all users
+     *
+     * @return a collection of all users
+     */
+    public Collection<User> findUsers();
+
+    /**
+     * Update the given user
+     *
+     * @param user
+     *            the user
+     */
+    public void updateUser(User user);
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/272b48aa/examples/camel-example-spring-boot-rest-swagger/src/main/java/org/apache/camel/example/springboot/UserServiceImpl.java
----------------------------------------------------------------------
diff --git a/examples/camel-example-spring-boot-rest-swagger/src/main/java/org/apache/camel/example/springboot/UserServiceImpl.java
b/examples/camel-example-spring-boot-rest-swagger/src/main/java/org/apache/camel/example/springboot/UserServiceImpl.java
new file mode 100644
index 0000000..b2ce33e
--- /dev/null
+++ b/examples/camel-example-spring-boot-rest-swagger/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<Integer, User>();
+
+    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);
+    }
+
+}


Mime
View raw message