MiinaK
MiinaK

Reputation: 39

Making a http request using Apache Camel

I'm having trouble getting started with Apache Camel. I'm trying to make a route which would make a http request to public API. I'm using a ready made project template and all the POM dependencies should be correct. Here's my code for the route:

    import org.apache.camel.builder.RouteBuilder;
    import org.springframework.stereotype.Component;

    @Component
    public class Routes extends RouteBuilder {
        @Override
        public void configure() {


            from("https://rata.digitraffic.fi/api/v1/train-
            locations/latest/")
            .description("Hello world -route")
            .log("Hello world!")
            .to("mock:out");


        }
    }

So I'm expecting to get some data from the API but now I'm just getting a build failure.

Upvotes: 1

Views: 2176

Answers (1)

vitorvr
vitorvr

Reputation: 655

I think you can't use the URL Request in from(). You need to create a route that the from is another event, like a Timer or consume a message from a JMS.

To make HTTP requests with Apache Camel, I use the component HTTP4 and declare the request on to().

<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-http4</artifactId>
  <version>${camel.version}</version>
</dependency>

Below an example with Timer component that every 15 seconds, the process is started and do a HTTP request.

@Component
public class Routes extends RouteBuilder {
    @Override
    public void configure() {

        from("timer:SimpleTimerName?period=15s")
        .description("Hello world -route")
        .log("Hello world!")
        .to("https4://rata.digitraffic.fi/api/v1/train-locations/latest/");
        .log("This is the status code from the response: ${header.CamelHttpResponseCode}")
        .log("This is the return: ${body}")
    }
}

Upvotes: 4

Related Questions