blob: 4abe095188812953db1663dc9acd0a840ef6283d [file]
== Spring Boot Example with Camel REST DSL and Platform HTTP
=== Introduction
This example illustrates how to use https://projects.spring.io/spring-boot/[Spring Boot] with http://camel.apache.org[Camel]. It provides a simple REST service that is created with http://camel.apache.org/rest-dsl.html[Camel REST DSL] and https://camel.apache.org/components/3.18.x/platform-http-component.html[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.
=== Build
You can build this example using:
[source,text]
----
mvn package
----
=== Run
You can run this example using:
[source,text]
----
mvn spring-boot:run
----
You should see the following output when the application is launched (timestamp removed for simplicity):
[source,text]
----
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: 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.22.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.22.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
[source,text]
----
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:
[source,json]
----
{"id":1,"title":"Todo title","completed":false,"order":1,"url":""}
----
Retrieve all TODOs
[source,text]
----
curl http://localhost:8080/todos
----
The command will produce the following output:
[source,json]
----
[{"id":1,"title":"Todo title","completed":false,"order":1,"url":""}]
----
Update one TODO
[source,text]
----
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:
[source,json]
----
{"id":1,"title":"Todo title","completed":true,"order":1,"url":""}
----
Delete completed TODOs
[source,text]
----
curl -X "DELETE" http://localhost:8080/todos
----
The command will produce the following output:
[source,json]
----
1
----
The Spring Boot application can be stopped pressing `[CTRL] + [C]` in the shell.
=== 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!