Skip to main content
AI
  • Get started with AI

    • Red Hat AI

      Accelerate the development and deployment of enterprise AI solutions.

    • AI learning hub

      Explore learning materials and tools, organized by task.

    • AI interactive demos

      Click through scenarios with Red Hat AI, including training LLMs and more.

    • AI/ML learning paths

      Expand your Red Hat OpenShift AI knowledge using these learning resources.

    • AI quickstarts

      Focused AI use cases designed for fast deployment on Red Hat AI platforms.

    • No-cost AI training

      Foundational Red Hat AI training.

  • Featured resources

    • Red Hat OpenShift AI
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

Red Hat AI Factory with NVIDIA

Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.

Learn
  • Self-guided

    • Documentation

      Find answers, get step-by-step guidance, and learn how to use Red Hat products.

    • Learning paths

      Explore curated walkthroughs for common development tasks.

    • Guided learning

      Receive custom learning paths powered by our AI assistant.

    • See all learning
  • Hands-on

    • Developer Sandbox

      Spin up Red Hat's products and technologies without setup or configuration.

    • Interactive labs

      Learn by doing in these hands-on, browser-based experiences.

    • Interactive demos

      Click through product features in these guided tours.

  • Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics
  • Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
Build
  • Get started

    • Red Hat build of Podman Desktop

      A downloadable, local development hub to experiment with our products and builds.

    • Developer Sandbox

      Spin up Red Hat's products and technologies without setup or configuration.

  • Download products

    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products
  • Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset
  • References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
Community
  • Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions
  • Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github
  • Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

Join the Red Hat Developer program

Download Red Hat products and project builds, access support documentation, learning content, and more.

ConsoleDocsSupport Search
New For you

Recommended

We'll recommend resources you may like as you browse. Try these suggestions for now.

  • Product downloads
  • Cheat sheets
  • Red Hat Developer learning portal
  • Developer blog
  • E-books
  • Developer Sandbox
Log in

Get more with a Red Hat Developer account

  • Product downloads
  • Console access
  • Event registration
  • Training & trials
Explore more account benefitsLog in or register

A subscription may be required for some services.

Contact us

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

    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

    • A day in the life of an NVIDIA H100 on Red Hat OpenShift 5

    • Migrate BuildConfigs to builds for Red Hat OpenShift with one plug-in install

    • Run Red Hat Enterprise Linux CoreOS on NVIDIA BlueField DPUs

    • Agent harness engineering: Separating model capabilities from enforcement

    • What's new for developers in Red Hat Enterprise Linux 10.3 and 9.9

    Red Hat Developers logo

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    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
    © 2026 Red Hat

    Red Hat legal and privacy links

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