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:

    <!-- use the same version as your Camel core version -->

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


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


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;

public class MyRestController {

	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;

	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:

public class MySpringBootRouter extends RouteBuilder {

    public void configure() {
        .process(exchange -> exchange.getIn().setBody(new Order(UUID.randomUUID().toString(), ThreadLocalRandom.current().nextInt(1, 10))))
        .setHeader(Exchange.HTTP_METHOD, constant("POST"))
        .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
        .process(exchange ->"HTTP POST response is: {}", exchange.getIn().getBody(String.class)));

        .process(exchange -> exchange.getIn().setBody(simple(null)))
        .setHeader(Exchange.HTTP_METHOD, constant("GET"))
        .setHeader(Exchange.HTTP_QUERY, constant("name=Frank"))
        .process(exchange ->"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;

public class MySpringBootApplication {

    public static void main(String[] args) {, 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:

Leave a comment