[JAVA] Hello World (REST API) with Apache Camel + Spring Boot 2


"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.

What is Apache Camel?

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.

Create a project in Eclipse

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". image.png

This completes the project creation.

Modify pom.xml

Modify pom.xml as follows.


<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">
  <name>camel-springboot-rest-test Maven Webapp</name>





Create a program

Create the main program as follows.


package mkyz08.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class Application {

    public static void main(String[] args) {

The following is a normal Spring Boot application launch.


I created the configuration file as follows, but it is okay not to create it because it is only the application name.


camel.springboot.name = MyCamelRestApp

Next, create a controller class (HelloRestController). Annotate the controller class (HelloRestController) with RestController.


public class HelloRestController {

The RequestMapping annotation maps the GET method to the request from the client.


    @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.


    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.


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;

public class HelloRestController {

	private ProducerTemplate producer = null;

	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.


package mkyz08.example;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

public class HelloWorldRoute extends RouteBuilder {

    public void configure() throws Exception {
				.routeId("hello world route")
				.setBody(simple("Hello World : ${body}"))

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 ")".

				.routeId("hello world route")
				.setBody(simple("Hello World : ${body}"))

Run the Hello World web application

Execute the created program and access the following URL.


"Hello World" is returned, and the following is also displayed on the standard output on the server side.

Hello World : hoge

Create an executable jar file

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] ------------------------------------------------------------------------
[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


Recommended Posts

Hello World (REST API) with Apache Camel + Spring Boot 2
Hello World (console app) with Apache Camel + Spring Boot 2
Hello World with Spring Boot
Hello World with Spring Boot!
Hello World with Spring Boot
Until "Hello World" with Spring Boot
(Intellij) Hello World with Spring Boot
Hello World with Eclipse + Spring Boot + Maven
(IntelliJ + gradle) Hello World with Spring Boot
Hello world! With Spring Boot (Marven + text editor)
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
[Spring Boot] Get user information with Rest API (beginner)
Implement a simple Rest API with Spring Security with Spring Boot 2.0
Customize REST API error response with Spring Boot (Part 2)
Customize REST API error response with Spring Boot (Part 1)
Spring with Kotorin --4 REST API design
Implement REST API in Spring Boot
Implement REST API with Spring Boot and JPA (Application Layer)
Implement REST API with Spring Boot and JPA (Infrastructure layer)
Implement REST API with Spring Boot and JPA (domain layer)
Implement a simple Rest API with Spring Security & JWT with Spring Boot 2.0
Implement a simple Web REST API server with Spring Boot + MySQL
Build REST API with Apache2 + Passenger + Sinatra.
Compare Hello, world! In Spring Boot with Java, Kotlin and Groovy
[Practice! ] Display Hello World in Spring Boot
How Spring Security works with Hello World
[Beginner] Let's write REST API of Todo application with Spring Boot
Spring Boot2 Web application development with Visual Studio Code Hello World creation
Hello World at explosive speed with Spring Initializr! !! !!
Try to display hello world with spring + gradle
Create a web api server with spring boot
Hello World with Micronaut
Download with Spring Boot
Sample to batch process data on DB with Apache Camel Spring Boot starters
Message cooperation started with Spring Boot Apache Kafka edition
Automatically map DTOs to entities with Spring Boot API
A memorandum when creating a REST service with Spring Boot
Introduce swagger-ui to REST API implemented in Spring Boot
Hello World comparison between Spark Framework and Spring Boot
Generate barcode with Spring Boot
Implement GraphQL with Spring Boot
Get started with Spring boot
Run LIFF with Spring Boot
SNS login with Spring Boot
Hello World with VS Code!
File upload with Spring Boot
Spring Boot starting with copy
Spring Boot starting with Docker
Set cookies with Spring Boot
REST API testing with REST Assured
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Link API with Spring + Vue.js
Create microservices with Spring Boot
Send email with spring boot
Hello World with SpringBoot / Gradle
Hello, World! With Asakusa Framework!
Handle Java 8 date and time API with Thymeleaf with Spring Boot
Create a Hello World web app with Spring framework + Jetty
Let's make a simple API with EC2 + RDS + Spring boot ①