Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Call an existing REST service with Apache Camel K

September 28, 2020
Mary Cochran
Related topics:
JavaKubernetesOperatorsServerless
Related products:
Red Hat build of Apache Camel

Share:

    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 and kamel 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 podsto 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

    Recent Posts

    • How to run AI models in cloud development environments

    • How Trilio secures OpenShift virtual machines and containers

    • How to implement observability with Node.js and Llama Stack

    • How to encrypt RHEL images for Azure confidential VMs

    • How to manage RHEL virtual machines with Podman Desktop

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue