You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by as...@apache.org on 2021/11/19 08:19:14 UTC
[camel-k] branch main updated: docs: add pojo-jar example
This is an automated email from the ASF dual-hosted git repository.
astefanutti pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git
The following commit(s) were added to refs/heads/main by this push:
new 119c1e0 docs: add pojo-jar example
119c1e0 is described below
commit 119c1e0dff06f07c549c48024a5de58f52fe3c08
Author: David Weber <da...@w3tec.ch>
AuthorDate: Fri Nov 5 15:41:06 2021 +0100
docs: add pojo-jar example
originally from https://github.com/dweber019/camel-k-jar
---
examples/pojo-jar/README.md | 31 ++++++
examples/pojo-jar/http/http-client.env.json | 5 +
examples/pojo-jar/http/open-api-route.http | 40 +++++++
examples/pojo-jar/pom.xml | 115 +++++++++++++++++++++
.../main/java/org/apache/camel/OpenApiRoute.java | 53 ++++++++++
.../src/main/java/org/apache/camel/User.java | 16 +++
.../main/java/org/apache/camel/UserService.java | 42 ++++++++
examples/pojo-jar/target/pojo-jar-1.0.0.jar | Bin 0 -> 5278 bytes
8 files changed, 302 insertions(+)
diff --git a/examples/pojo-jar/README.md b/examples/pojo-jar/README.md
new file mode 100644
index 0000000..9a863e3
--- /dev/null
+++ b/examples/pojo-jar/README.md
@@ -0,0 +1,31 @@
+# camel-k-jar
+This example ports the following examples to Camel K:
+- https://camel.apache.org/manual/rest-dsl.html#_openapi_swagger_api
+- https://github.com/apache/camel-examples/tree/camel-3.11.x/examples/openapi-cdi
+
+Target of this setup was to make the local development experience better,
+by providing a way to use not only inner classes in route file (`OpenApiRoute.java`) and avoid using
+Maven or jitpack, which add major round trips to the development cycle.
+
+## Solution
+### Build a JAR
+You can build the JAR file by `mvn install`.
+Be aware that you have to exclude the route file (`OpenApiRoute.java`) in the `pom.xml` as this file will already be provided over the
+`kamel run ...` CLI command.
+
+### Include the JAR
+Now that you have the source code packed in the jar file you have to provide this jar file to the Camel K container,
+where your Camel K route is running.
+You can do this by adding the following (modelines)[https://camel.apache.org/camel-k/1.6.x/cli/modeline.html]
+to your route file (`OpenApiRoute.java`).
+```
+// camel-k: resource=file:../../../../../target/pojo-jar-1.0.0.jar
+// camel-k: trait=jvm.classpath=/etc/camel/resources/pojo-jar-1.0.0.jar
+```
+It's important to add the JAR file as resource to be compressed.
+
+### Run this route
+To run this integration use the following command:
+```bash
+kamel run ./src/main/org/apache/camel/OpenApiRoute.java
+```
diff --git a/examples/pojo-jar/http/http-client.env.json b/examples/pojo-jar/http/http-client.env.json
new file mode 100644
index 0000000..eecfb0a
--- /dev/null
+++ b/examples/pojo-jar/http/http-client.env.json
@@ -0,0 +1,5 @@
+{
+ "dev": {
+ "host": "https://your-host"
+ }
+}
diff --git a/examples/pojo-jar/http/open-api-route.http b/examples/pojo-jar/http/open-api-route.http
new file mode 100644
index 0000000..1ba0162
--- /dev/null
+++ b/examples/pojo-jar/http/open-api-route.http
@@ -0,0 +1,40 @@
+###
+# All users
+#
+GET {{host}}/users
+Content-Type: application/json
+
+###
+# One user
+#
+GET {{host}}/users/123
+Content-Type: application/json
+
+###
+# Get department
+#
+# => 404 does not respect Accept and doesn't care about param type
+#
+GET {{host}}/users/123/departments/david
+Content-Type: application/json
+Accept: application/json
+
+###
+# Update user
+#
+# => if body not sent 400
+# => Validations e.g. NotBlank, NotNull
+#
+PUT {{host}}/users
+Content-Type: application/json
+
+{
+ "id": 123,
+ "name": "Joe Ding"
+}
+
+###
+# OpenApi => does not work
+#
+GET {{host}}/api-doc/openapi.yaml
+Content-Type: application/json
\ No newline at end of file
diff --git a/examples/pojo-jar/pom.xml b/examples/pojo-jar/pom.xml
new file mode 100644
index 0000000..9249724
--- /dev/null
+++ b/examples/pojo-jar/pom.xml
@@ -0,0 +1,115 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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>
+
+ <groupId>org.apache.camel</groupId>
+ <artifactId>pojo-jar</artifactId>
+ <packaging>jar</packaging>
+ <version>1.0.0</version>
+
+ <name>A Camel K with pojo jar</name>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+ <log4j2-version>2.13.3</log4j2-version>
+ <camel-version>3.12.0</camel-version>
+ </properties>
+
+ <dependencyManagement>
+ <dependencies>
+ <!-- Camel BOM -->
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-bom</artifactId>
+ <version>${camel-version}</version>
+ <scope>import</scope>
+ <type>pom</type>
+ </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-jackson</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-openapi-java</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.projectlombok</groupId>
+ <artifactId>lombok</artifactId>
+ <version>1.18.22</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <!-- testing -->
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-test-junit5</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.projectlombok</groupId>
+ <artifactId>lombok</artifactId>
+ <version>RELEASE</version>
+ <scope>compile</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <defaultGoal>install</defaultGoal>
+
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>3.8.1</version>
+ <configuration>
+ <source>1.8</source>
+ <target>1.8</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-resources-plugin</artifactId>
+ <version>3.2.0</version>
+ <configuration>
+ <encoding>UTF-8</encoding>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jar-plugin</artifactId>
+ <version>3.2.0</version>
+ <configuration>
+ <archive>
+ <manifest>
+ <addClasspath>true</addClasspath>
+ <classpathPrefix>libs/</classpathPrefix>
+ </manifest>
+ </archive>
+ <excludes>
+ **/*Route*
+ </excludes>
+ </configuration>
+ </plugin>
+
+ </plugins>
+ </build>
+
+</project>
diff --git a/examples/pojo-jar/src/main/java/org/apache/camel/OpenApiRoute.java b/examples/pojo-jar/src/main/java/org/apache/camel/OpenApiRoute.java
new file mode 100644
index 0000000..187d25b
--- /dev/null
+++ b/examples/pojo-jar/src/main/java/org/apache/camel/OpenApiRoute.java
@@ -0,0 +1,53 @@
+// camel-k: name=pojo-jar
+// camel-k: dependency=camel-jackson
+// camel-k: dependency=camel-openapi-java
+// camel-k: dependency=mvn:org.projectlombok:lombok:1.18.22
+// camel-k: resource=file:../../../../../../target/pojo-jar-1.0.0.jar
+// camel-k: trait=jvm.classpath=/etc/camel/resources/pojo-jar-1.0.0.jar
+
+package org.apache.camel;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.rest.RestBindingMode;
+import org.apache.camel.model.rest.RestParamType;
+
+@Slf4j
+public class OpenApiRoute extends RouteBuilder {
+ @Override
+ public void configure() {
+ getContext().getRegistry().bind("userService", new UserService());
+
+ restConfiguration()
+ .component("platform-http")
+ .bindingMode(RestBindingMode.json)
+ .dataFormatProperty("prettyPrint", "true")
+ .apiContextPath("/api-doc")
+ .apiProperty("api.title", "User API").apiProperty("api.version", "1.2.3")
+ .apiProperty("cors", "true")
+ .clientRequestValidation(true);
+
+ rest("/users").description("User rest service")
+ .consumes("application/json").produces("application/json")
+
+ .get("/{id}").description("Find user by id").outType(User.class)
+ .param().name("id").type(RestParamType.path).description("The id of the user to get").dataType("integer").endParam()
+ .responseMessage().code(200).message("The user").endResponseMessage()
+ .to("bean:userService?method=getUser(${header.id})")
+
+ .put().description("Updates or create a user").type(User.class)
+ .param().name("body").type(RestParamType.body).description("The user to update or create").required(true).endParam()
+ .responseMessage().code(200).message("User created or updated").endResponseMessage()
+ .to("bean:userService?method=updateUser")
+
+ .get().description("Find all users").outType(User[].class)
+ .responseMessage().code(200).message("All users").endResponseMessage()
+ .to("bean:userService?method=listUsers")
+
+ .get("/{id}/departments/{did}").description("Find all users").outType(User[].class)
+ .param().name("id").type(RestParamType.path).description("The id of the user to get").dataType("integer").endParam()
+ .param().name("did").type(RestParamType.path).description("The id of the department to get").dataType("integer").required(true).endParam()
+ .responseMessage().code(200).message("All users").endResponseMessage()
+ .to("bean:userService?method=listUsers");
+ }
+}
diff --git a/examples/pojo-jar/src/main/java/org/apache/camel/User.java b/examples/pojo-jar/src/main/java/org/apache/camel/User.java
new file mode 100644
index 0000000..99460a9
--- /dev/null
+++ b/examples/pojo-jar/src/main/java/org/apache/camel/User.java
@@ -0,0 +1,16 @@
+package org.apache.camel;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import javax.validation.constraints.NotNull;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class User {
+ private int id;
+ @NotNull
+ private String name;
+}
diff --git a/examples/pojo-jar/src/main/java/org/apache/camel/UserService.java b/examples/pojo-jar/src/main/java/org/apache/camel/UserService.java
new file mode 100644
index 0000000..7d5861b
--- /dev/null
+++ b/examples/pojo-jar/src/main/java/org/apache/camel/UserService.java
@@ -0,0 +1,42 @@
+package org.apache.camel;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.Random;
+import java.util.TreeMap;
+
+@Slf4j
+public class UserService {
+ private final Map<String, User> users = new TreeMap<>();
+ private Random ran = new Random();
+ public UserService() {
+ users.put("123", new User(123, "John Doe"));
+ users.put("456", new User(456, "Donald Duck"));
+ users.put("789", new User(789, "Slow Turtle"));
+ }
+
+ public User getUser(String id) {
+ log.info("getUser", id);
+ if ("789".equals(id)) {
+ int delay = 500 + ran.nextInt(1500);
+ try {
+ Thread.sleep(delay);
+ } catch (Exception e) {
+ // ignore
+ }
+ }
+ return users.get(id);
+ }
+
+ public Collection<User> listUsers() {
+ log.info("listUsers");
+ return users.values();
+ }
+
+ public void updateUser(User user) {
+ log.info("updateUser", user);
+ users.put("" + user.getId(), user);
+ }
+}
diff --git a/examples/pojo-jar/target/pojo-jar-1.0.0.jar b/examples/pojo-jar/target/pojo-jar-1.0.0.jar
new file mode 100644
index 0000000..d6ee3c4
Binary files /dev/null and b/examples/pojo-jar/target/pojo-jar-1.0.0.jar differ