"REST API" of Hello World with Apache Camel + Spring Boot 2. The console application version will be posted as follows.
Hello World with Apache Camel + Spring Boot 2 (console app)
It is a REST API to create, but when accessing "/ spring / hello", it returns "Hello World". Also, the contents of the request are displayed asynchronously in the standard output on the server side.
It is created by combining the following frameworks.
I don't think Spring Boot needs explanation, so I will explain only Apache Camel briefly.
Apache Camel is often described as middleware (framework) for integration between systems. Integration between systems has a best practice called Enterprise Integration Patterns, which Camel makes it easy. Writing like this sounds a little difficult, and I feel that the threshold is unnecessarily high. In reality, you can think of it as a framework that makes it easy to "receive a request from another system, perform some processing, and send / store (DB or queue) somewhere." REST / SOAP, database (JDBC, MongoDB, DynamoDB, etc), file, message queue (ActiveMQ, Kafka, RabbitMQ, etc), mail, MQTT, etc. can be linked with various things, and parts to realize it Is called a component. There are nearly 200 components, and you can usually find the one you want to connect to. The "do some processing" part also covers functions such as data conversion, format conversion, routing, and error handling, and is a framework that can be used in any application.
First, create a new Maven project. Select "Maven Project" and click "Next".
"Next" without any particular changes.
Select "maven-archetype-quickstart" and click "Next".
Enter the project information and click "Finish".
This completes the project creation.
Modify pom.xml as follows.
pom.xml
<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>mkyz08.sample</groupId>
<artifactId>camel-springboot-rest-test</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>camel-springboot-rest-test Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.source>${java.version}</maven.compiler.source>
<camel-version>2.23.0</camel-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-parent</artifactId>
<version>${camel-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-stream</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Create the main program as follows.
Application.java
package mkyz08.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication(Application.class).run(args);
}
}
The following is a normal Spring Boot application launch.
SpringApplication(Application.class).run(args);
I created the configuration file as follows, but it is okay not to create it because it is only the application name.
src/main/resources/application.properties
camel.springboot.name = MyCamelRestApp
Next, create a controller class (HelloRestController). Annotate the controller class (HelloRestController) with RestController.
HelloRestController.java
@RestController
@RequestMapping("/spring")
public class HelloRestController {
The RequestMapping annotation maps the GET method to the request from the client.
HelloRestController.java
@RequestMapping(method = RequestMethod.GET, value = "/hello",
produces = "text/plain")
In the hello method, the received msg parameter value is stored in the asynchronous queue of "seda: hello_world". If you want to synchronize, you can change it just by setting "direct: hello_world". And it returns the string "Hello World" as a response.
HelloRestController.java
public String hello(String msg) {
Endpoint end = context.getEndpoint("seda:hello_world");
producer.sendBody(end, msg);
return "Hello World";
The whole source code of the controller class (HelloRestController) is as follows.
HelloRestController.java
package mkyz08.example;
import javax.annotation.Resource;
import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.apache.camel.ProducerTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/spring")
public class HelloRestController {
@Resource
private ProducerTemplate producer = null;
@Resource
private CamelContext context;
@RequestMapping(method = RequestMethod.GET, value = "/hello",
produces = "text/plain")
public String hello(String msg) {
Endpoint end = context.getEndpoint("seda:hello_world");
producer.sendBody(end, msg);
return "Hello World";
}
}
Finally, create a Camel route. Because of Camel's route definition, Component annotation is added to the class and it inherits RouteBuilder class. Then override the configure method and code the contents of the route.
HelloWorldRoute.java
package mkyz08.example;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
@Component
public class HelloWorldRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
from("seda:hello_world")
.routeId("hello world route")
.setBody(simple("Hello World : ${body}"))
.to("stream:out");
}
}
Below is the content of the route
--Start processing received requests asynchronously with "from (" seda: hello_world ")" --". RouteId (" hello world route ")", which only specifies the route ID --". SetBody (simple (" Hello World: $ {body} "))", set the value of the parameter (msg) received in the BODY of Exchange --The value of the parameter (msg) contained in the BODY of Exchange is output to the console with ".to (" stream: out ")".
from("seda:hello_world")
.routeId("hello world route")
.setBody(simple("Hello World : ${body}"))
.to("stream:out");
Execute the created program and access the following URL.
http://localhost:8080/spring/hello?msg=hoge
"Hello World" is returned, and the following is also displayed on the standard output on the server side.
Hello World : hoge
To create an executable jar file, enter "clean install spring-boot: repackage" in the goal in "Execution Configuration" and execute it as shown below.
If the execution is successful, the following log will be output.
[INFO] --- spring-boot-maven-plugin:2.1.0.RELEASE:repackage (default-cli) @ camel-springboot-rest-test ---
[INFO] Replacing main artifact C:\pleiades\workspace\camel-springboot-rest-test\target\camel-springboot-rest-test-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.017 s
[INFO] Finished at: 2018-12-06T18:39:38+09:00
[INFO] ------------------------------------------------------------------------```
The executable jar file is created in the "target" and directory of the project, and can be executed with the following command.
java -jar camel-springboot-rest-test-0.0.1-SNAPSHOT.jar
#reference
Recommended Posts