Writing Your First Camel Spring Boot Project With the Rest DSL

Writing Your First Camel Spring Boot Project With the Rest DSL

Rest services are becoming more and more popular for communication between systems.  Now that Red Hat supports the use of Red Hat JBoss Fuse with Apache Camel Spring Boot, learn how you can get started with the Rest DSL and Spring Boot.  These directions will use the camel-servlet component, although various components can be used.

As with all Spring Boot projects, you will first need a class to start up the application:

@SpringBootApplication
@Configuration
@ComponentScan("com.sample.camel")
public class SampleCamelApplication {
  /**
  * A main method to start this application.
  */
  public static void main(String[] args) {
    SpringApplication.run(SampleCamelApplication.class, args);
  }

  @Bean
  public ServletRegistrationBean camelServletRegistrationBean() {
    ServletRegistrationBean registration = new ServletRegistrationBean(new CamelHttpTransportServlet(), "/camel/*");
    registration.setName("CamelServlet");
    return registration;
  }
 }

Next you can write your camel route.  This example extends the RouteBuilder class:

@Component
public class SampleCamelRouter 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 from the command line using mvn spring-boot:run. Then you can see if your service is working by hitting the following url: http://localhost:8080/camel/hello

Come out to my workshop with Claus Ibsen at Red Hat Summit to learn more about the Rest DSL, how to deploy this route (and more) to Red Hat OpenShift, and how to use 3scale by Red Hat to manage your APIs.

Develop using Red Hat's most valuable products

Your membership unlocks Red Hat products and technical training on enterprise cloud application development.

JOIN RED HAT DEVELOPER

Download and learn more about Red Hat JBoss Fuse, an innovative modular, cloud-ready architecture, powerful management and automation, and world class developer productivity. It is Java™ EE 7 certified and features powerful, enterprise-grade features such as high availability clustering, distributed caching, messaging, transactions, and a full web services stack.

Share