You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2017/04/27 08:29:10 UTC
[3/5] camel git commit: just trying out camel and the geocoder
component
just trying out camel and the geocoder component
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/8e27c018
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/8e27c018
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/8e27c018
Branch: refs/heads/master
Commit: 8e27c018dbc2fc0068c551c809c7c7546dae8d1b
Parents: 260bb1c
Author: manos <ma...@sobol>
Authored: Wed Apr 26 00:50:26 2017 +0300
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Apr 27 09:30:36 2017 +0200
----------------------------------------------------------------------
examples/README.adoc | 4 +-
.../README.adoc | 118 +++++++++++++++++++
.../camel-example-spring-boot-geocoder/pom.xml | 115 ++++++++++++++++++
.../springboot/geocoder/Application.java | 42 +++++++
.../springboot/geocoder/CamelRouter.java | 55 +++++++++
.../springboot/geocoder/ApplicationTest.java | 59 ++++++++++
examples/pom.xml | 1 +
7 files changed, 393 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/8e27c018/examples/README.adoc
----------------------------------------------------------------------
diff --git a/examples/README.adoc b/examples/README.adoc
index e9b2635..bec6b5b 100644
--- a/examples/README.adoc
+++ b/examples/README.adoc
@@ -11,7 +11,7 @@ View the individual example READMEs for details.
### Examples
// examples: START
-Number of Examples: 90 (9 deprecated)
+Number of Examples: 91 (9 deprecated)
[width="100%",cols="4,2,4",options="header"]
|=======================================================================
@@ -172,6 +172,8 @@ Number of Examples: 90 (9 deprecated)
| link:camel-example-spark-rest/README.md[Spark REST] (camel-example-spark-rest) | Rest | An example using Spark REST
+| link:camel-example-spring-boot-geocoder/README.adoc[Spring Boot Geocoder] (camel-example-spring-boot-geocoder) | Rest | An example showing Camel REST DSL and Swagger with Spring Boot
+
| link:camel-example-spring-boot-rest-swagger/README.adoc[Spring Boot Rest Swagger] (camel-example-spring-boot-rest-swagger) | Rest | An example showing Camel REST DSL and Swagger with Spring Boot
| link:camel-example-swagger-cdi/README.md[Swagger CDI] (camel-example-swagger-cdi) | Rest | An example using REST DSL and Swagger Java with CDI
http://git-wip-us.apache.org/repos/asf/camel/blob/8e27c018/examples/camel-example-spring-boot-geocoder/README.adoc
----------------------------------------------------------------------
diff --git a/examples/camel-example-spring-boot-geocoder/README.adoc b/examples/camel-example-spring-boot-geocoder/README.adoc
new file mode 100644
index 0000000..25f628d
--- /dev/null
+++ b/examples/camel-example-spring-boot-geocoder/README.adoc
@@ -0,0 +1,118 @@
+# Spring Boot Example with Camel REST DSL, Geocoder 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], using the http://camel.apache.org/geocoder.html[Geocoder component] and documented with http://swagger.io[Swagger].
+
+The project uses the `camel-spring-boot-starter` and `camel-geocoder-starter` dependencies as Spring Boot starter dependencies for Camel that simplify the Maven configuration.
+
+The project was based on it's `camel-example-spring-boot-rest-swagger` sibling.
+
+### 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 request the address for "Paris": http://localhost:8080/camel/geocoder/?address=Paris
+
+You can also access the REST endpoint from the command line:
+
+ $ curl http://localhost:8080/camel/geocoder/?address=Paris
+
+
+The command will produce the following output:
+
+----
+{
+ "status" : "OK",
+ "results" : [ {
+ "types" : [ "locality", "political" ],
+ "formattedAddress" : "Paris, France",
+ "addressComponents" : [ {
+ "longName" : "Paris",
+ "shortName" : "Paris",
+ "types" : [ "locality", "political" ]
+ }, {
+ "longName" : "Paris",
+ "shortName" : "Paris",
+ "types" : [ "administrative_area_level_2", "political" ]
+ }, {
+ "longName" : "�le-de-France",
+ "shortName" : "�le-de-France",
+ "types" : [ "administrative_area_level_1", "political" ]
+ }, {
+ "longName" : "France",
+ "shortName" : "FR",
+ "types" : [ "country", "political" ]
+ } ],
+ "geometry" : {
+ "location" : {
+ "lat" : 48.856614,
+ "lng" : 2.3522219
+ },
+ "locationType" : "APPROXIMATE",
+ "viewport" : {
+ "southwest" : {
+ "lat" : 48.815573,
+ "lng" : 2.225193
+ },
+ "northeast" : {
+ "lat" : 48.9021449,
+ "lng" : 2.4699208
+ }
+ },
+ "bounds" : {
+ "southwest" : {
+ "lat" : 48.815573,
+ "lng" : 2.224199
+ },
+ "northeast" : {
+ "lat" : 48.9021449,
+ "lng" : 2.4699208
+ }
+ }
+ },
+ "partialMatch" : false
+ } ]
+}
+----
+
+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/8e27c018/examples/camel-example-spring-boot-geocoder/pom.xml
----------------------------------------------------------------------
diff --git a/examples/camel-example-spring-boot-geocoder/pom.xml b/examples/camel-example-spring-boot-geocoder/pom.xml
new file mode 100644
index 0000000..adf7ce5
--- /dev/null
+++ b/examples/camel-example-spring-boot-geocoder/pom.xml
@@ -0,0 +1,115 @@
+<?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-geocoder</artifactId>
+ <name>Camel :: Example :: Spring Boot with Geocoder and REST DSL</name>
+ <description>An example showing the Camel Geocoder component via REST DSL 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</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-geocoder-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/8e27c018/examples/camel-example-spring-boot-geocoder/src/main/java/org/apache/camel/example/springboot/geocoder/Application.java
----------------------------------------------------------------------
diff --git a/examples/camel-example-spring-boot-geocoder/src/main/java/org/apache/camel/example/springboot/geocoder/Application.java b/examples/camel-example-spring-boot-geocoder/src/main/java/org/apache/camel/example/springboot/geocoder/Application.java
new file mode 100644
index 0000000..5896f1a
--- /dev/null
+++ b/examples/camel-example-spring-boot-geocoder/src/main/java/org/apache/camel/example/springboot/geocoder/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.geocoder;
+
+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/8e27c018/examples/camel-example-spring-boot-geocoder/src/main/java/org/apache/camel/example/springboot/geocoder/CamelRouter.java
----------------------------------------------------------------------
diff --git a/examples/camel-example-spring-boot-geocoder/src/main/java/org/apache/camel/example/springboot/geocoder/CamelRouter.java b/examples/camel-example-spring-boot-geocoder/src/main/java/org/apache/camel/example/springboot/geocoder/CamelRouter.java
new file mode 100644
index 0000000..3f0183a
--- /dev/null
+++ b/examples/camel-example-spring-boot-geocoder/src/main/java/org/apache/camel/example/springboot/geocoder/CamelRouter.java
@@ -0,0 +1,55 @@
+/**
+ * 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.geocoder;
+
+import com.google.code.geocoder.model.GeocodeResponse;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.rest.RestBindingMode;
+import org.springframework.stereotype.Component;
+
+import static org.apache.camel.model.rest.RestParamType.query;
+
+/**
+ * A simple Camel REST DSL route example using the Geocoder component and documented with Swagger
+ *
+ */
+@Component
+public class CamelRouter extends RouteBuilder {
+
+ @Override
+ public void configure() throws Exception {
+ restConfiguration()
+ .component("servlet")
+ .bindingMode(RestBindingMode.json)
+ .dataFormatProperty("prettyPrint", "true")
+ .apiContextPath("/api-doc")
+ .apiProperty("api.title", "Geocoder API").apiProperty("api.version", "1.0.0")
+ .apiProperty("cors", "true");
+
+
+ rest("/geocoder").description("Geocoder REST service")
+ .consumes("application/json")
+ .produces("application/json")
+
+ .get().description("Geocoder address lookup").outType(GeocodeResponse.class)
+ .param().name("address").type(query).description("The address to lookup").dataType("string").endParam()
+ .responseMessage().code(200).message("Geocoder successful").endResponseMessage()
+ .toD("geocoder:address:${header.address}");
+
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/8e27c018/examples/camel-example-spring-boot-geocoder/src/test/java/org/apache/camel/example/springboot/geocoder/ApplicationTest.java
----------------------------------------------------------------------
diff --git a/examples/camel-example-spring-boot-geocoder/src/test/java/org/apache/camel/example/springboot/geocoder/ApplicationTest.java b/examples/camel-example-spring-boot-geocoder/src/test/java/org/apache/camel/example/springboot/geocoder/ApplicationTest.java
new file mode 100644
index 0000000..fb7a3a4
--- /dev/null
+++ b/examples/camel-example-spring-boot-geocoder/src/test/java/org/apache/camel/example/springboot/geocoder/ApplicationTest.java
@@ -0,0 +1,59 @@
+/**
+ * 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.geocoder;
+
+
+import com.google.code.geocoder.model.GeocodeResponse;
+import com.google.code.geocoder.model.GeocoderStatus;
+import org.apache.camel.CamelContext;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
+import org.springframework.boot.test.web.client.TestRestTemplate;
+import org.springframework.core.ParameterizedTypeReference;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.test.context.junit4.SpringRunner;
+
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
+public class ApplicationTest {
+
+ @Autowired
+ private TestRestTemplate restTemplate;
+
+ @Autowired
+ private CamelContext camelContext;
+
+ @Test
+ public void geocoderAddressTest() {
+ ResponseEntity<GeocodeResponse> response = restTemplate.exchange("/camel/geocoder?address=Paris",
+ HttpMethod.GET, null, new ParameterizedTypeReference<GeocodeResponse>() { });
+ assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
+ GeocodeResponse res = response.getBody();
+ assertThat(res.getStatus()).isEqualTo(GeocoderStatus.OK);
+ assertThat(res.getResults()).isNotEmpty();
+ assertThat(res.getResults()).element(0)
+ .hasFieldOrPropertyWithValue("formattedAddress", "Paris, France");
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/camel/blob/8e27c018/examples/pom.xml
----------------------------------------------------------------------
diff --git a/examples/pom.xml b/examples/pom.xml
index 8a5fdbb..c85d821 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -91,6 +91,7 @@
<module>camel-example-splunk</module>
<module>camel-example-spring</module>
<module>camel-example-spring-boot</module>
+ <module>camel-example-spring-boot-geocoder</module>
<module>camel-example-spring-boot-live-reload</module>
<module>camel-example-spring-boot-metrics</module>
<module>camel-example-spring-boot-rest-jpa</module>