Explore, build, test and deploy a Camel Spring Boot demo application using the Developer Sandbox and OpenShift Dev Spaces.
This hands-on lab is based on the following blog article in Red Hat Developers:
Assuming you have followed the article’s instructions, you should be all set to get hands-on with Camel Spring Boot in the OpenShift Dev Spaces workspace.
For illustration purposes, the picture below shows what the integration end-to-end flow looks like.
A client invokes an OpenApi service. A Camel route attends the call, translates the JSON input into XML and calls a backend service to obtain an XML response, then it’s translated back to JSON before responding to the original service call.
The Camel source file api-simple.xml
defines the entire end-to-end processing logic, which you can find in your project explorer under the path:
Inside the Camel source you’ll see the main route definition:
The key processing actions are:
-
Performs the JSON to XML transformation
-
Invokes the backend service
-
Transforms the XML response into JSON
The code above is written using the XML DSL (Domain Specific Language), but Camel also provides a Java DSL and a YAML DSL.
Feel free to explore other regions of the code and project if you are curious about the entire implementation.
The stub acts as the backend service that provides the XML data we need to fetch.
-
Open your terminal
Make sure you make your terminal visible in the IDE. You can toggle it using the keyboard keys Ctrl+` or simply find the option from the menu system as per the picture below:
-
Let’s first run the stub
Copy and paste the following command in your terminal to place yourself in the stub’s Camel Spring Boot project:
cd camelsb/stubs/end1
Then, copy/paste the following command to start the stub in the terminal:
mvn -Dspring-boot.run.profiles=dev -s configuration/settings.xml
Now, test your stub from a new terminal. From your terminal’s top right corner, choose the Split option, as shown below:
Copy/paste the following cURL command to obtain a response from the stub:
curl -s \ -H "content-type: application/xml" \ -d '' \ http://localhost:9000/camel/subscriber/details \ | bat -pP -lxml
NoteThe command also includes a pipe to colorize the XML output for better reading. The invocation should return an XML payload similar to:
<Individual> <Name>Some</Name> <Surname>One</Surname> <Address> <Number>1</Number> <Street>Some Street</Street> <City>Somewhere</City> <PostCode>SOME C0D3</PostCode> <Country>UK</Country> </Address> </Individual>
Did you obtain the same XML message as shown above?
You’ve successfully tested the stub !!
Inspect in the stub logs to investigate the possible causes of failure.
The main Camel service exposes a JSON REST API and integrates with the XML backend service (the stub).
-
Run the main service
Copy and paste the following command in your terminal to place yourself in the main Camel Spring Boot project:
cd /projects/devsandbox-camel/camelsb/level1simple/
Then, copy/paste the following command to start the stub in the terminal:
mvn -Dspring-boot.run.profiles=dev -s configuration/settings.xml
Open a third terminal from which you can issue commands. From your terminal’s top right corner, choose the Split option as shown below:
Copy/paste the following cURL command to obtain a response from the stub:
curl -s \ -H "content-type: application/json" \ -d '{"id":"123"}' \ http://localhost:8080/camel/subscriber/details | jq
NoteThe command includes a pipe to parse the JSON response with JQuery, which nicely renders the returned JSON payload. The cURL command should return a JSON payload similar to:
{ "fullName": "Some One", "addressLine1": "1 Some Street", "addressLine2": "Somewhere SOME C0D3", "addressLine3": "UK" }
Did you obtain the same JSON response as the one shown above?
You’ve successfully tested the main service !!
Inspect in the stub logs to investigate possible causes of failure.
The stub acts as the backend service that provides the XML data we need to fetch.
-
Stop both systems
Make sure you stop both the stub and the main service by selecting each terminal and pressing the keys Ctrl+c. Your view of your terminals should look like:
-
Deploy the stub
Make sure your CLI
oc
client (OpenShift client) points to your Developer Sandbox project (aka namespace):oc projects -q | grep dev | xargs oc project
NoteThe Developer Sandbox only allows 1 project (namespace) per user. The command above should output something similar to:
Now using project "<your-username>-dev" on server "https://172.30.0.1:443".
WarningNot specifying your target project (namespace) in OpenShift may result in a deployment failure. You can now copy and paste the following command in your terminal to trigger the deployment:
mvn oc:deploy -Popenshift -s configuration/settings.xml
-
You’ll see Maven fetching more dependencies and then interact with OpenShift to finalise the deployment of the stub.
When done, if successful, going back to your browser’s tab with your OpenShift’s developer topology view, you should see the new service up and ready when fully started, looking similar to:
-
-
Test the stub
Copy/paste the following cURL command to obtain a response from the stub:
curl -s \ -H "content-type: application/xml" \ -d '' \ http://end1:8080/camel/subscriber/details \ | bat -pP -lxml
NoteThe cURL command above now points to the newly deployed pod, with its service end1
listening on port 8080.NoteThe command also includes a pipe to colorise the XML output for better reading. The invocation should return an XML payload similar to:
<Individual> <Name>Some</Name> <Surname>One</Surname> <Address> <Number>1</Number> <Street>Some Street</Street> <City>Somewhere</City> <PostCode>SOME C0D3</PostCode> <Country>UK</Country> </Address> </Individual>
Did you obtain the same XML message as shown above?
You’ve successfully tested the stub deployed in the sandbox !!
Inspect in the stub logs to investigate possible causes of failure.
With the stub already deployed, we just need to deploy the service which will integrate with the stub running under the same namespace.
-
Deploy the service
Ensure you run the commands below from the terminal located in the path of your main service project.
You can now copy and paste the following command in your terminal to trigger the deployment:
mvn oc:deploy -Popenshift -s configuration/settings.xml
-
Test the service
Copy/paste the following cURL command to obtain a response from the
simple
service:curl -s \ -H "content-type: application/json" \ -d '{"id":"123"}' \ http://simple:8080/camel/subscriber/details | jq
NoteThe cURL command above now points to the newly deployed pod’s Kubernetes service simple
, listening on port 8080.NoteThe command also includes a pipe to parse and colorise the JSON output for better reading. The invocation should return a JSON payload similar to:
{ "fullName": "Some One", "addressLine1": "1 Some Street", "addressLine2": "Somewhere SOME C0D3", "addressLine3": "UK" }
-
Invoke the service as an external client
Notice the previous cURL command uses an internal service URL, which is not directly accessible by external consumers. However, the deployment automatically creates a route in OpenShift that exposes the service to external clients.
You can obtain the route details with the following command and use its URL from your favourite local HTTP client/tester, like Postman, Swagger or others.
oc get route simple
Embedding the
oc get route
command in a cURL allows you to invoke the service as an external consumer.-
Copy/paste the following cURL to obtain the service OpenAPI definition:
curl -s http://`oc get route simple -o jsonpath={.spec.host}`/camel/openapi.json | jq
The invocation should return the OpenApi specification, similar to:
{ "openapi": "3.0.2", "info": { "title": "Subscriber", "version": "1.0.0" }, ...
-
Copy/paste the following cURL command to simulate an external call and obtain a response from the Camel service:
curl -s \ -H "content-type: application/json" \ -d '{"id":"123"}' \ http://`oc get route simple -o jsonpath={.spec.host}`/camel/subscriber/details | jq
The invocation should return a JSON payload similar to:
{ "fullName": "Some One", "addressLine1": "1 Some Street", "addressLine2": "Somewhere SOME C0D3", "addressLine3": "UK" }
-
Did you obtain the same JSON response as shown above?
You’ve successfully invoked the simple service as an external client !!
Inspect in the stub logs to investigate possible causes of failure.
When you’re done playing in the Developer Sandbox, you can clean up your Sandbox namespace by un-deploying your Camel simple
service and stub end1
using the following Maven oc:undeploy
command for both:
mvn oc:undeploy -Popenshift -s configuration/settings.xml
Executing the command above for both services should leave your topology view clean from routes, services, and other Kubernetes artifacts in your namespace.
Is your namespace clean from artifacts?
You’ve successfully cleaned up your namespace !!
Inspect in the logs to investigate possible causes of failure.