With the release of Apache Camel K, it is possible to create and deploy integrations with existing applications that are quicker and more lightweight than ever. In many cases, calling an existing REST endpoint is the best way to connect a new system to an existing one. Take the example of a cafe serving coffee. What happens when the cafe wants to allow customers to use a delivery service like GrubHub? You would only need to introduce a single Camel K integration to connect the cafe and GrubHub systems.
In this article, I will show you how to create a Camel K integration that calls an existing REST service and uses its existing data format. For the data format, I have a Maven project configured with Java objects. Ideally, you would have this packaged and available in a Nexus repository. For the purpose of my demonstration, I utilized JitPack, which lets me have my dependency available in a repository directly from my GitHub code. See the GitHub repository associated with this demo for the data format code and directions for getting it into JitPack.
Prerequisites
In order to follow the demonstration, you will need the following installed in your development environment:
- The
oc
command-line tools - Camel K client 1.0.0
- A Red Hat OpenShift 4.4 cluster
Create the Camel K route
First, we create the Camel K route file, which I have named RestWithUndertow.java
. Here, we open the file and create the class structure:
public class RestWithUndertow extends org.apache.camel.builder.RouteBuilder { @Override public void configure() throws Exception { } }
Next, we create the REST endpoint, and we also create the data formats that we will use. In this case, we'll receive the REST request as a GrubHubOrder
. We'll then transform it to a CreateOrderCommand
, which we'll send to the REST service that is already in use:
import org.apache.camel.model.rest.RestBindingMode; import com.redhat.quarkus.cafe.domain.CreateOrderCommand; import com.redhat.grubhub.cafe.domain.GrubHubOrder; import org.apache.camel.component.jackson.JacksonDataFormat; public class RestWithUndertow extends org.apache.camel.builder.RouteBuilder { @Override public void configure() throws Exception { JacksonDataFormat df = new JacksonDataFormat(CreateOrderCommand.class); rest() .post("/order").type(GrubHubOrder.class).consumes("application/json") .bindingMode(RestBindingMode.json) .produces("application/json") .to("direct:order"); } }
Create the data transformation
Now we can create a method in the same file, which will assist with the data transformation from the GrubHubOrder
to the CreateOrderCommand
:
public void transformMessage(Exchange exchange){ Message in = exchange.getIn(); GrubHubOrder gho = in.getBody(GrubHubOrder.class); List oi = gho.getOrderItems(); List list = new ArrayList(); for(GrubHubOrderItem i : oi){ LineItem li = new LineItem(Item.valueOf(i.getOrderItem()),i.getName()); list.add(li); } CreateOrderCommand coc = new CreateOrderCommand(list, null); in.setBody(coc); }
Make sure that you add the following imports to the file, as well:
import org.apache.camel.Exchange; import org.apache.camel.Message; import com.redhat.quarkus.cafe.domain.LineItem; import com.redhat.quarkus.cafe.domain.Item; import java.util.List; import java.util.ArrayList; import com.redhat.grubhub.cafe.domain.GrubHubOrderItem;
Call the existing service from your Camel K REST endpoint
Now that we have a method to do the transformation, we can implement the rest of the Camel K REST endpoint and make it call the existing service. Add the following below the code that you have so far:
from("direct:order") .log("Incoming Body is ${body}") .log("Incoming Body after unmarshal is ${body}") .bean(this,"transformMessage") .log("Outgoing pojo Body is ${body}") .marshal(df) //transforms the java object into json .setHeader(Exchange.HTTP_METHOD, constant("POST")) .setHeader(Exchange.CONTENT_TYPE, constant("application/json")) .setHeader("Accept",constant("application/json")) .log("Body after transformation is ${body} with headers: ${headers}") .to("http://?bridgeEndpoint=true&throwExceptionOnFailure=false") .setHeader(Exchange.HTTP_RESPONSE_CODE,constant(200)) .transform().simple("{Order Placed}");
Note that this example includes plenty of logging to give visibility to what is being done. I have set the BridgeEndpoint
option to true
, which allows us to ignore the HTTP_URI
incoming header and use the full URL that we've specified. This is important when taking an incoming REST request that will call a new one. You can read more about the BridgeEndpoint
option here.
Save the route file
Your complete Camel K route file should look something like this:
import org.apache.camel.Exchange; import org.apache.camel.Message; import org.apache.camel.model.rest.RestBindingMode; import com.redhat.quarkus.cafe.domain.LineItem; import com.redhat.quarkus.cafe.domain.Item; import java.util.List; import java.util.ArrayList; import com.redhat.quarkus.cafe.domain.CreateOrderCommand; import com.redhat.grubhub.cafe.domain.GrubHubOrder; import com.redhat.grubhub.cafe.domain.GrubHubOrderItem; import org.apache.camel.component.jackson.JacksonDataFormat; public class RestWithUndertow extends org.apache.camel.builder.RouteBuilder { @Override public void configure() throws Exception { JacksonDataFormat df = new JacksonDataFormat(CreateOrderCommand.class); rest() .post("/order").type(GrubHubOrder.class).consumes("application/json") .bindingMode(RestBindingMode.json) .produces("application/json") .to("direct:order"); from("direct:order") .log("Incoming Body is ${body}") .log("Incoming Body after unmarshal is ${body}") .bean(this,"transformMessage") .log("Outgoing pojo Body is ${body}") .marshal(df) .setHeader(Exchange.HTTP_METHOD, constant("POST")) .setHeader(Exchange.CONTENT_TYPE, constant("application/json")) .setHeader("Accept",constant("application/json")) .log("Body after transformation is ${body} with headers: ${headers}") //need to change url after knowing what the cafe-web url will be .to("http://sampleurl.com?bridgeEndpoint=true&throwExceptionOnFailure=false") .setHeader(Exchange.HTTP_RESPONSE_CODE,constant(200)) .transform().simple("{Order Placed}"); } public void transformMessage(Exchange exchange){ Message in = exchange.getIn(); GrubHubOrder gho = in.getBody(GrubHubOrder.class); List oi = gho.getOrderItems(); List list = new ArrayList(); for(GrubHubOrderItem i : oi){ LineItem li = new LineItem(Item.valueOf(i.getOrderItem()),i.getName()); list.add(li); } CreateOrderCommand coc = new CreateOrderCommand(list, null); in.setBody(coc); } }
Save this file and get ready for the last step.
Run the integration
To run your integration, you will need to have the Camel K Operator installed and ensure that it has access to the dependencies in JitPack. Do the following to get your infrastructure ready for the integration:
- Log in to OpenShift using the
oc
command line. - Install the Camel K Operator via the OpenShift OperatorHub. The default options are fine.
- Ensure you have Kamel CLI tooling
- Use the
oc
andkamel
tools and the following command to create an integration platform that provides access to JitPack:kamel install --olm=false --skip-cluster-setup --skip-operator-setup --maven-repository https://jitpack.io@id=jitpack@snapshots
Once you see that everything is ready in your OpenShift console (you can always enter oc get pods
to check), deploy the integration. Using your Kamel tools again, make sure that you are logged into OpenShift and on the appropriate project, and run the following command:
kamel run --name=rest-with-undertow --dependency=camel-jackson --dependency=mvn:com.github.jeremyrdavis:quarkus-cafe-demo:1.5-SNAPSHOT --dependency=mvn:com.github.jeremyrdavis.quarkus-cafe-demo:grubhub-cafe-core:1.5-SNAPSHOT --dependency=camel-openapi-java RestWithUndertow.java
This command ensures that your route starts with all of the appropriate dependencies. See the GitHub repository for this article for a complete, working version of this code and the service that it calls.
Conclusion
Camel K allows you to develop your integrations quickly and efficiently while keeping your footprint small. Even when you have some dependencies for your integrations you can utilize the Knative technology in Camel K to make your integrations less resource intensive and allow for quicker deployments.
Last updated: January 22, 2024