1

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.

1 Answer 1

4

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}")
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.