How to use Camel HTTP Component

In this tutorial we will learn how to send HTTP Request to a REST Service using Camel and Spring Boot.

Camel HTTP Component provides support for calling external HTTP resources. You can typically use it to invoke REST Services.

To use the HTTP Component, you need to add the following dependency to your Camel standalone project:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-http</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

On the other hand, if you are running Camel with Spring Boot, you can include the camel-http-starter in your pom.xml:

<dependency>
   <groupId>org.apache.camel.springboot</groupId>
   <artifactId>camel-http-starter</artifactId>
</dependency>

In addition, if you need to perform JSON Marshalling, then include also the following starter:

<dependency>
      <groupId>org.apache.camel.springboot</groupId>
      <artifactId>camel-gson-starter</artifactId>
</dependency>

Let’s create a sample Spring Boot Project to show the Camel HTTP Component in action.

Setting up the Spring Boot Project

Our sample Spring Boot application includes a REST Controller with a GET and a POST resource:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyRestController {

	@PostMapping("/order")
	public String postBody(@RequestBody Order order) {
		System.out.println("Invoked rest with " + order);
		String note = "Item ordered on " + new java.util.Date().toString();
		return note;
	}

	@GetMapping("/hello")
	public String postBody(@RequestParam String name) {
		return "Hello " + name;
	}
}

The above RestController contains two endpoints:

  • The @PostMapping endpoint receives as input a the Order object which is marshalled from a JSON String and returns a String with the current Date
  • The @GetMapping endpoint returns a simple String.

To test both Camel HTTP GET and Camel HTTP POST we will add two Routes in a Spring @Component which extends RouteBuilder:

@Component
public class MySpringBootRouter extends RouteBuilder {

    @Override
    public void configure() {
    	from("timer://test1?period=2000")
        .process(exchange -> exchange.getIn().setBody(new Order(UUID.randomUUID().toString(), ThreadLocalRandom.current().nextInt(1, 10))))
        .marshal().json(JsonLibrary.Gson)
        .setHeader(Exchange.HTTP_METHOD, constant("POST"))
        .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
        .to("http://localhost:8080/order")
        .process(exchange -> log.info("HTTP POST response is: {}", exchange.getIn().getBody(String.class)));

   
    	
    	from("timer://test2?period=1000")
        .process(exchange -> exchange.getIn().setBody(simple(null)))
        .setHeader(Exchange.HTTP_METHOD, constant("GET"))
        .setHeader(Exchange.HTTP_QUERY, constant("name=Frank"))
        .to("http://localhost:8080/hello")
        .process(exchange -> log.info("HTTP GET response is: {}", exchange.getIn().getBody(String.class)));
    }
 

}

As you can see, the HTTP POST fires every two seconds according to a Timer. It creates an Order object with random data. The Order object is marshalled, the Headers set and finally we invoke the HTTP Component.

The HTTP GET is simpler as we don’t need any marshalling. There’s a String which enters the Endpoint as HTTP Query and the Response is logged.

A SpringBootApplication class will be your entrypoint:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MySpringBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApplication.class, args);
    }

}

Finally, let’s run our demo!

Running the Project

You can run your Spring Boot project as usual with:

mvn install spring-boot:run

Check the output:

Invoked rest with Order [item=0ba9af68-d8fc-4776-9243-f97b930d2294, quantity=5]
2022-01-10 12:32:08.574  INFO 31330 --- [- timer://test1] com.sample.MySpringBootRouter            : HTTP POST response is: Item ordered on Mon Jan 10 12:32:08 CET 2022
2022-01-10 12:32:08.574  INFO 31330 --- [- timer://test2] com.sample.MySpringBootRouter            : HTTP GET response is: Hello Frank
2022-01-10 12:32:09.570  INFO 31330 --- [- timer://test2] com.sample.MySpringBootRouter            : HTTP GET response is: Hello Frank

The Source code for this tutorial is available here: https://github.com/fmarchioni/masterspringboot/tree/master/camel/camel-http

Leave a comment