Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

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

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Red Hat 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
    • See all technologies
    • Programming languages & frameworks

      • Java
      • Python
      • JavaScript
    • System design & architecture

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

      • Productivity
      • Tools
      • GitOps
    • Automated data processing

      • AI/ML
      • Data science
      • Apache Kafka on Kubernetes
    • Platform engineering

      • DevOps
      • DevSecOps
      • Red Hat Ansible Automation Platform for applications and services
    • Secure development & architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & cloud native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • See all learning resources

    E-books

    • GitOps cookbook
    • Podman in action
    • Kubernetes operators
    • The path to GitOps
    • See all e-books

    Cheat sheets

    • Linux commands
    • Bash commands
    • Git
    • systemd commands
    • See all cheat sheets

    Documentation

    • Product documentation
    • API catalog
    • Legacy documentation
  • 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 the 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

The Camel Rest DSL

February 2, 2017
Mary Cochran
Related topics:
Developer Tools
Related products:
Red Hat Fuse

    Camel

    Apache Camel is a piece of JBoss Fuse.  It is an open source integration framework with a variety of components to fit your integration needs.  Camel is a Java-based implementation of the Enterprise Integration Patterns based on a book by Gregor Hohpe and Bobby Woolf.  Camel includes components for HTTP, Files, FTP, JMS, JDBC, AWS, and much more.  While Camel can be used for many different purposes, this post will focus on the REST DSL specifically.

    Rest DSL Overview

    Apache Camel provides a quick and easy way to get REST endpoints up and running without a dependency on CXF by using the Rest DSL.  Red Hat support for the Rest DSL came with the release of JBoss Fuse 6.2. Before Camel 2.15, most Rest implementation was done using the CXFRS Camel component.  While the CXFRS component did its job, many found it complex and confusing.  It required more files to be maintained than the new Rest DSL and made the application dependent on CXF.  The following Camel components support the use of the Rest DSL:

    • camel-coap
    • camel-netty-http
    • camel-netty4-http
    • camel-jetty
    • camel-restlet
    • camel-servlet
    • camel-spark-rest
    • camel-undertow

    Rest DSL Syntax

    As with any Camel route, if using the Java DSL, the class must extend the RouteBuilder class and implement a configure() method to form the route definition. One must specify which Camel component the route should use in the restConfiguration() call.  In this configuration, the servlet component is being used.  The binding mode is set to enforce the use of json.  You can leave it as auto so that the rest endpoint can process json, xml, or something else.  Next, the endpoint can be defined.  The root URL will be defined in using syntax like rest ("/user").  Then you can specify what type of call can be made to the endpoint: GET, PUT, POST, or DELETE. What the endpoint produces and consumes may also be defined in addition to defining what type of object the endpoint is expecting to be produced or consumed.  From there, the route is defined the same as any other Camel route.

     @Override
     public void configure() throws Exception {
         restConfiguration().component("servlet").bindingMode(RestBindingMode.json);
    
         rest("/user")
             .get("/{id}").produces("application/json").to("direct:getUser")
             .post().consumes("application/json").type(User.class).to("direct:createUser")
             .delete("/{id}").to("direct:deleteUser");
    
         from("direct:getUser")
             .log("GET /user/${header.id} request received!")
             .setBody(simple("${header.id}"))
             .to("bean:usersBean?method=getUser");
    
         from("direct:createUser")
             .log("POST /user/ request received!")
             .to("bean:usersBean?method=createUser");
    
         from("direct:deleteUser")
             .log("DELETE /user/${header.id} request received!")
             .setBody(simple("${header.id}"))
             .to("bean:usersBean?method=deleteUser");
     }
    
    

    Rest DSL on JBoss EAP

    In order to get your Rest DSL Camel route running properly on EAP using CDI, you will need to add a servlet to the web.xml.  Please note the camel-servlet is the only supported way of using the Rest DSL with CDI on JBoss EAP at the current time.  If you want to use the Rest DSL with Spring on EAP I would suggest packing the Camel jars in your war.  If you choose to use CDI, the only edits needed are to the web.xml and then an additional annotation on your route class.

    web.xml

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
       <listener>
          <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
       </listener>
    
       <!-- Camel Servlet -->
       <servlet>
          <servlet-name>CamelServlet</servlet-name>
          <servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class>
          <load-on-startup>1</load-on-startup>
       </servlet>
    
       <!-- Camel Servlet Mapping -->
       <servlet-mapping>
          <servlet-name>CamelServlet</servlet-name>
          <url-pattern>/rest/*</url-pattern>
       </servlet-mapping>
    </web-app>

    route class

    @ContextName("rest-dsl")
    public class RestRoutes extends RouteBuilder {}

    Rest DSL on JBoss Fuse Karaf

    In order to get your Rest DSL route running on Karaf, you will need to have a camel-context.xml file.  Aside from this file, you will need to have proper import-packages and export-packages configured for your bundle.  This is a standard OSGi configuration.  See the JBoss Fuse documentation for more information on import and export packages.

    camel-context.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
     xmlns:jaxws="http://cxf.apache.org/blueprint/jaxws" xmlns:camel="http://camel.apache.org/schema/blueprint"
     xsi:schemaLocation="
     http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
     http://cxf.apache.org/blueprint/jaxws http://cxf.apache.org/schemas/blueprint/jaxws.xsd
     http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
     ">
    
       <!-- The camel context which registers the route -->
       <camel:camelContext id="fusequickstart-restdsl-camel" xmlns="http://camel.apache.org/schema/blueprint">
          <!-- Package Scanning finds the Routes -->
          <packageScan>
             <package>com.redhat.consulting.fusequickstarts.karaf.rest.dsl.route</package>
          </packageScan>
       </camel:camelContext>
    
    </blueprint>

    These examples and more can found in full at https://github.com/rhtconsulting/fuse-quickstarts.

    Last updated: February 23, 2024

    Recent Posts

    • Run privileged commands more securely in OpenShift Dev Spaces

    • Advanced authentication and authorization for MCP Gateway

    • Unify OpenShift Service Mesh observability: Perses and Prometheus

    • Visualize Performance Co-Pilot data with geomaps in Grafana

    • Integrate a custom AI service with Red Hat Ansible Lightspeed

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

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

    Red Hat legal and privacy links

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

    Report a website issue