Get started with REST services with Apache Camel

Get started with REST services with Apache Camel

REST services can be used for data access APIs, sending information from devices to the cloud, monitoring, and more.

Cloud
Image by : 
x

Get the newsletter

Join the 85,000 open source advocates who receive our giveaway alerts and article roundups.

REST services are becoming an increasingly popular architectural style for connecting modern systems with the cloud and to each other as the need for flexible APIs and microservices grows. With Apache Camel, you can write REST services easier and quicker using the REST domain-specific language (DSL).

During a poster session at the Grace Hopper Celebration of Women in Computing (September 26 - 28, 2018 in Houston), we will walk audiences through developing their first Camel route using the REST DSL.

Apache Camel is a leading open source integration framework. Design patterns are common solutions used in development that have been proven to work effectively for a given problem. Camel is a Java-based implementation of Enterprise Integration Patterns (EIPs), established design patterns and integration solutions for common system connection and data exchange concerns.

We have seen REST services used for data access APIs, sending information from devices to the cloud, monitoring, and even migrating accounts at credit card companies. The Camel REST DSL allows the creation of REST services using Restlet, Servlet, Jetty, and a variety of other HTTP-aware components for implementation. Using the REST DSL allows the creation of REST services quicker and easier than ever.

Getting started

The quickest way to get started with the Camel Rest DSL (in our opinion) is using Spring Boot. Spring Boot provides auto-configuration for Apache Camel to quickly create standalone applications.

To enable Apache Camel in the Spring Boot application, we need the camel-spring-boot-starter dependency. In Maven this would be:

<dependency>
   <groupId>org.apache.camel</groupId>  
   <artifactId>camel-spring-boot-starter</artifactId>
</dependency>

Next, as with all Spring Boot applications, you will need to set up a class to start up the application.

@SpringBootApplication
@Configuration
@ComponentScan("com.simple.camel.project")
public class MySimpleCamelApplication {
  /**
  * A main method to start this application.
  **/
  public static void main(String[] args) {
    SpringApplication.run(MySimpleCamelApplication.class, args);
  }
  /**
  * from camel 2.21.0 on this ServletRegistrationBean is not required
  * and the path defaults to /camel
  **/
  @Bean
  public ServletRegistrationBean camelServletRegistrationBean() {
    ServletRegistrationBean registration = new ServletRegistrationBean(new CamelHttpTransportServlet(), "/camel/*");
    registration.setName("CamelServlet");
    return registration;
  }
 }

Camel's main feature is a routing engine. Camel has two main ways of defining routing rules: Java DSL and XML. We will be focusing on the Java-based DSL. You can write your Camel REST DSL route by extending the Camel RouteBuilder class and implementing the configure method, like this:

@Component
public class MySimpleCamelRouter extends RouteBuilder {
  @Override
  public void configure() throws Exception {
    restConfiguration()
      .component("servlet")
      .bindingMode(RestBindingMode.json);

    rest().get("/hello")
      .to("direct:hello");
 
    from("direct:hello")
      .log(LoggingLevel.INFO, "Hello World")
      .transform().simple("Hello World");
   }
}

Now you can run your example using mvn spring-boot:run and hit your new REST API at http://localhost:8080/camel/hello. It's easy to test REST services from a web browser when testing GET services, as those can be accessed via the URL in the address bar. POST, PUT, and DELETE are much harder. You can install third-party plugins that provide a REST client (such as Postman) to try other REST verbs.

Check here for a more in-depth lab and example around creating a Rest API with Camel, deploying it to OpenShift, and managing it with 3Scale.

Learn more

Our presentation at the Grace Hopper Celebration of Women in Computing will be part of Poster Session 3 on September 27 from 11:30 a.m. to 2 p.m. It will be technical—while we would like our session to be applicable for beginners, some previous knowledge of Java and/or integration is helpful.

Grace Hopper is a fantastic opportunity to network with women in technology, learn from others, and experience the women in the technology community as a whole. This is Mary's second year speaking and attending Grace Hopper and Krystal's first year speaking. Come to visit us during the poster symposium!

About the author

Mary Cochran - Mary Cochran is a Middleware Solutions Architect for Red Hat. She has a background in hands on development and experience with a wide array of Integration products. She is an Apache Camel contributor and a frequent tech blogger. In addition, Mary has had the pleasure of speaking at many conferences from Red Hat Summit to API World to Grace Hopper Celebration of Women in computing.

About the author

Krystal Ying - Krystal Ying is a Jamaican-raised, Cincinnati-transplant from south Florida and former EWF INI Fellowship recipient. She is currently a Middleware Consultant for Red Hat. Prior to joining Red Hat, She was employed as an Software Engineer at Lexmark International, Inc. She holds a Masters in Information Technology - Information Security from Carnegie Mellon University and Bachelors in Electrical and Computer Engineering with a Minor in Computer Science from Cornell University. When she’s not off...