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 2022/08/16 07:25:35 UTC
[camel-examples] branch main updated: CAMEL-6645: A camel-mapstruct example
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-examples.git
The following commit(s) were added to refs/heads/main by this push:
new 77537fe3 CAMEL-6645: A camel-mapstruct example
77537fe3 is described below
commit 77537fe322fc209c8148de8b6815f7052bd0f84e
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Aug 16 09:25:20 2022 +0200
CAMEL-6645: A camel-mapstruct example
---
examples/mapstruct/README.adoc | 70 +++++++++
examples/mapstruct/pom.xml | 169 +++++++++++++++++++++
.../org/apache/camel/example/MyApplication.java | 36 +++++
.../org/apache/camel/example/MyBeanEnricher.java | 27 ++++
.../java/org/apache/camel/example/MyRoute.java | 46 ++++++
.../apache/camel/example/mapper/BeerMapper.java | 31 ++++
.../src/main/resources/application.properties | 23 +++
.../src/main/resources/documents/beer.json | 13 ++
.../src/main/resources/documents/beverage.json | 6 +
examples/mapstruct/src/main/resources/logback.xml | 30 ++++
examples/pom.xml | 1 +
11 files changed, 452 insertions(+)
diff --git a/examples/mapstruct/README.adoc b/examples/mapstruct/README.adoc
new file mode 100644
index 00000000..2d090c74
--- /dev/null
+++ b/examples/mapstruct/README.adoc
@@ -0,0 +1,70 @@
+== Camel Example MapStruct
+
+This example shows how to use MapStruct with Camel.
+
+https://mapstruct.org/[MapStruct] is a powerful Java bean mapping code generator.
+
+=== How to use MapStruct
+
+1. This example generates POJO classes from sample JSon data located in src/main/resources/documents.
+We use the jsonschema2pojo-maven-plugin Maven plugin (see details in `pom.xml`).
+
+2. Then we have installed MapStruct as a Java compiler plugin (see details in `pom.xml`).
+
+3. We declared the MapStruct mapping in the `BeerMapper` Java interface.
+
+4. We configured camel-mapstruct to load the mappings by package scanning (see details in `application.properties`)
+
+=== Build
+
+First compile the example by executing:
+
+[source,sh]
+----
+$ mvn compile
+----
+
+=== How to run
+
+Then you can run this example using
+
+[source,sh]
+----
+$ mvn camel:run
+----
+
+And then send an HTTP POST request to Camel which will do a JSON to JSON mapping via MapStruct.
+
+[source,bash]
+----
+curl -s -X POST -H "Content-Type: application/json" -d @src/main/resources/documents/beer.json http://localhost:8080/beer
+----
+
+NOTE: The sample data are from https://random-data-api.com/api/beer/random_beer
+
+
+=== How to configure for Camel Textual Route debugging
+
+Several IDEs are providing support for Camel Textual Route debugging. To enable this possibility, you need to launch this example with the profile `camel.debug`.
+
+[source,sh]
+----
+$ mvn camel:run -Pcamel.debug
+----
+
+This profile can also be activated with camel.debug property set to true. For instance, by setting the property from command-line:
+
+[source,sh]
+----
+$ mvn camel:run -Dcamel.debug=true
+----
+
+=== Help and contributions
+
+If you hit any problem using Camel or have some feedback, then please
+https://camel.apache.org/community/support/[let us know].
+
+We also love contributors, so
+https://camel.apache.org/community/contributing/[get involved] :-)
+
+The Camel riders!
diff --git a/examples/mapstruct/pom.xml b/examples/mapstruct/pom.xml
new file mode 100644
index 00000000..7beebd9f
--- /dev/null
+++ b/examples/mapstruct/pom.xml
@@ -0,0 +1,169 @@
+<?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/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.camel.example</groupId>
+ <artifactId>examples</artifactId>
+ <version>3.19.0-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>camel-example-mapstruct</artifactId>
+ <packaging>jar</packaging>
+ <name>Camel :: Example :: MapStruct</name>
+ <description>How to use MapStruct for POJO data mapping</description>
+
+ <properties>
+ <category>Transformation</category>
+ </properties>
+
+ <dependencyManagement>
+ <dependencies>
+ <!-- Add Camel BOM -->
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-bom</artifactId>
+ <version>${camel.version}</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
+ </dependencies>
+ </dependencyManagement>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-main</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-mapstruct</artifactId>
+ </dependency>
+ <!-- we use json with jackson -->
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-jackson</artifactId>
+ </dependency>
+ <!-- embed as http server -->
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-undertow</artifactId>
+ </dependency>
+
+ <!-- logging -->
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-api</artifactId>
+ <version>${log4j2-version}</version>
+ <scope>runtime</scope>
+ </dependency>
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-core</artifactId>
+ <version>${logback-version}</version>
+ </dependency>
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-classic</artifactId>
+ <version>${logback-version}</version>
+ </dependency>
+ <!-- for testing -->
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-test-main-junit5</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-maven-plugin</artifactId>
+ <version>${camel.version}</version>
+ <configuration>
+ <logClasspath>false</logClasspath>
+ <mainClass>org.apache.camel.example.MyApplication</mainClass>
+ </configuration>
+ </plugin>
+ <!-- to use mapstruct we need to use its compiler-plugin -->
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>${maven-compiler-plugin-version}</version>
+ <configuration>
+ <compilerArgs>
+ <arg>-parameters</arg>
+ </compilerArgs>
+ <annotationProcessorPaths>
+ <path>
+ <groupId>org.mapstruct</groupId>
+ <artifactId>mapstruct-processor</artifactId>
+ <version>${mapstruct-version}</version>
+ </path>
+ </annotationProcessorPaths>
+ </configuration>
+ </plugin>
+ <!-- generate DTO from json example files -->
+ <plugin>
+ <groupId>org.jsonschema2pojo</groupId>
+ <artifactId>jsonschema2pojo-maven-plugin</artifactId>
+ <version>1.1.2</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>generate</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <sourceDirectory>${basedir}/src/main/resources/documents</sourceDirectory>
+ <targetPackage>org.apache.camel.example.model</targetPackage>
+ <sourceType>json</sourceType>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ <profiles>
+ <profile>
+ <id>camel.debug</id>
+ <activation>
+ <property>
+ <name>camel.debug</name>
+ <value>true</value>
+ </property>
+ </activation>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-debug</artifactId>
+ <version>${camel.version}</version>
+ </dependency>
+ </dependencies>
+ </profile>
+ </profiles>
+</project>
diff --git a/examples/mapstruct/src/main/java/org/apache/camel/example/MyApplication.java b/examples/mapstruct/src/main/java/org/apache/camel/example/MyApplication.java
new file mode 100644
index 00000000..31197dc0
--- /dev/null
+++ b/examples/mapstruct/src/main/java/org/apache/camel/example/MyApplication.java
@@ -0,0 +1,36 @@
+/*
+ * 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 org.apache.camel.main.Main;
+
+/**
+ * Main class that boot the Camel application
+ */
+public final class MyApplication {
+
+ private MyApplication() {
+ }
+
+ public static void main(String[] args) throws Exception {
+ // use Camels Main class
+ Main main = new Main(MyApplication.class);
+ // now keep the application running until the JVM is terminated (ctrl + c or sigterm)
+ main.run(args);
+ }
+
+}
diff --git a/examples/mapstruct/src/main/java/org/apache/camel/example/MyBeanEnricher.java b/examples/mapstruct/src/main/java/org/apache/camel/example/MyBeanEnricher.java
new file mode 100644
index 00000000..635289ed
--- /dev/null
+++ b/examples/mapstruct/src/main/java/org/apache/camel/example/MyBeanEnricher.java
@@ -0,0 +1,27 @@
+/*
+ * 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 org.apache.camel.example.model.Beverage;
+
+public class MyBeanEnricher {
+
+ public void enrich(Beverage bev) {
+ // we can also use regular Java to modify the pojo
+ bev.setCategory("beer");
+ }
+}
diff --git a/examples/mapstruct/src/main/java/org/apache/camel/example/MyRoute.java b/examples/mapstruct/src/main/java/org/apache/camel/example/MyRoute.java
new file mode 100644
index 00000000..5241bf8c
--- /dev/null
+++ b/examples/mapstruct/src/main/java/org/apache/camel/example/MyRoute.java
@@ -0,0 +1,46 @@
+/*
+ * 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 org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.example.model.Beer;
+import org.apache.camel.example.model.Beverage;
+import org.apache.camel.model.rest.RestBindingMode;
+
+public class MyRoute extends RouteBuilder {
+
+ @Override
+ public void configure() throws Exception {
+ // use json binding in rest service
+ restConfiguration()
+ .host("localhost")
+ .port(8080)
+ .bindingMode(RestBindingMode.json);
+
+ rest("/beer")
+ .post()
+ .type(Beer.class)
+ .outType(Beverage.class)
+ .to("direct:beer");
+
+ from("direct:beer")
+ // use type converter which triggers mapstruct to map from Beer to Beverage
+ .convertBodyTo(Beverage.class)
+ .bean(MyBeanEnricher.class);
+
+ }
+}
diff --git a/examples/mapstruct/src/main/java/org/apache/camel/example/mapper/BeerMapper.java b/examples/mapstruct/src/main/java/org/apache/camel/example/mapper/BeerMapper.java
new file mode 100644
index 00000000..4105df26
--- /dev/null
+++ b/examples/mapstruct/src/main/java/org/apache/camel/example/mapper/BeerMapper.java
@@ -0,0 +1,31 @@
+/*
+ * 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.mapper;
+
+import org.apache.camel.example.model.Beer;
+import org.apache.camel.example.model.Beverage;
+import org.mapstruct.Mapper;
+import org.mapstruct.Mapping;
+
+@Mapper
+public interface BeerMapper {
+
+ @Mapping(source = "style", target = "category")
+ @Mapping(source = "alcohol", target = "strength")
+ Beverage toBeverage(Beer beer);
+
+}
diff --git a/examples/mapstruct/src/main/resources/application.properties b/examples/mapstruct/src/main/resources/application.properties
new file mode 100644
index 00000000..27607e3b
--- /dev/null
+++ b/examples/mapstruct/src/main/resources/application.properties
@@ -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.
+## ---------------------------------------------------------------------------
+
+# here you can configure options on camel main
+# https://camel.apache.org/components/next/others/main.html
+camel.main.name = BeerCamel
+
+# configure camel-mapstruct
+camel.component.mapstruct.mapper-package-name = org.apache.camel.example.mapper
diff --git a/examples/mapstruct/src/main/resources/documents/beer.json b/examples/mapstruct/src/main/resources/documents/beer.json
new file mode 100644
index 00000000..7f9419aa
--- /dev/null
+++ b/examples/mapstruct/src/main/resources/documents/beer.json
@@ -0,0 +1,13 @@
+{
+ "id": 2060,
+ "uid": "64b36188-ac50-436f-9804-286828459aac",
+ "brand": "Birra Moretti",
+ "name": "Sierra Nevada Celebration Ale",
+ "style": "English Brown Ale",
+ "hop": "Nugget",
+ "yeast": "3763 - Roeselare Ale Blend",
+ "malts": "Carapils",
+ "ibu": "92 IBU",
+ "alcohol": "5.3%",
+ "blg": "12.8°Blg"
+}
\ No newline at end of file
diff --git a/examples/mapstruct/src/main/resources/documents/beverage.json b/examples/mapstruct/src/main/resources/documents/beverage.json
new file mode 100644
index 00000000..c58dd943
--- /dev/null
+++ b/examples/mapstruct/src/main/resources/documents/beverage.json
@@ -0,0 +1,6 @@
+{
+ "kind": "beer",
+ "name": "Sierra Nevada Celebration Ale",
+ "category": "English Brown Ale",
+ "strength": "5.3%"
+}
\ No newline at end of file
diff --git a/examples/mapstruct/src/main/resources/logback.xml b/examples/mapstruct/src/main/resources/logback.xml
new file mode 100644
index 00000000..a798d0b3
--- /dev/null
+++ b/examples/mapstruct/src/main/resources/logback.xml
@@ -0,0 +1,30 @@
+<?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>
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ <encoder>
+ <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
+ </encoder>
+ </appender>
+
+ <root level="INFO">
+ <appender-ref ref="STDOUT" />
+ </root>
+</configuration>
diff --git a/examples/pom.xml b/examples/pom.xml
index 7596462b..22c120b7 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -133,6 +133,7 @@
<module>main-xml</module>
<module>main-yaml</module>
<module>management</module>
+ <moudle>mapstruct</moudle>
<module>mongodb</module>
<module>netty-custom-correlation</module>
<module>oaipmh</module>