This example illustrates how to use Spring Boot with Camel. It provides a simple REST service that is created with Camel REST DSL and platform-http.
The project uses the camel-spring-boot-starter
dependency, a Spring Boot starter dependency for Camel that simplifies the Maven configuration.
The project also uses camel-servlet-starter
component as the implementation for platform-http-engine.
You can run this example using:
mvn spring-boot:run
You should see the following output when the application is launched (timestamp removed for simplicity):
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.4.5)
o.a.c.example.springboot.Application : Starting Application using Java 17.0.15 with PID 3707237
o.a.c.example.springboot.Application : No active profile set, falling back to 1 default profile: "default"
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
o.apache.catalina.core.StandardService : Starting service [Tomcat]
o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.40]
o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 451 ms
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/'
o.a.c.impl.engine.AbstractCamelContext : Apache Camel 4.12.0-SNAPSHOT (MyCamel) is starting
o.a.c.impl.engine.AbstractCamelContext : Routes startup (total:6 rest-dsl:6)
o.a.c.impl.engine.AbstractCamelContext : Started route1 (rest://get:/todos)
o.a.c.impl.engine.AbstractCamelContext : Started route2 (rest://get:/todos:/%7Bid%7D)
o.a.c.impl.engine.AbstractCamelContext : Started route3 (rest://patch:/todos:/%7Bid%7D)
o.a.c.impl.engine.AbstractCamelContext : Started route4 (rest://post:/todos)
o.a.c.impl.engine.AbstractCamelContext : Started route5 (rest://delete:/todos)
o.a.c.impl.engine.AbstractCamelContext : Started route6 (rest://delete:/todos:/%7Bid%7D)
o.a.c.impl.engine.AbstractCamelContext : Apache Camel 4.12.0-SNAPSHOT (MyCamel) started in 8ms (build:0ms init:0ms start:8ms boot:428ms)
o.a.c.example.springboot.Application : Started Application in 1.155 seconds (process running for 1.283)
After the Spring Boot application is started, you can execute the following HTTP requests:
Create a TODO
curl -d '{"title":"Todo title", "completed":"false", "order": 1, "url":""}' -H "Content-Type: application/json" -X POST http://localhost:8080/todos
The command will produce the following output:
{"id":1,"title":"Todo title","completed":false,"order":1,"url":""}
Retrieve all TODOs
curl http://localhost:8080/todos
The command will produce the following output:
[{"id":1,"title":"Todo title","completed":false,"order":1,"url":""}]
Update one TODO
curl -d '{"title":"Todo title", "completed":"true", "order": 1, "url":""}' -H "Content-Type: application/json" -X PATCH http://localhost:8080/todos/1
The command will produce the following output:
{"id":1,"title":"Todo title","completed":true,"order":1,"url":""}
Delete completed TODOs
curl -X "DELETE" http://localhost:8080/todos
The command will produce the following output:
1
The Spring Boot application can be stopped pressing [CTRL] + [C]
in the shell.
If you hit any problem using Camel or have some feedback, then please let us know.
We also love contributors, so get involved :-)
The Camel riders!