Search code examples
javaspringrestcxfrs

Invoke external REST API using Camel and Spring


The question may be generic but it's quite just what the title says.

I have an external API using HTTPS which I need to invoke within a Camel route to get some JSON response back however, I cannot seem to find a good way to do this.

I tried Camel's component 'restlet' to invoke the API but no luck. I tried to make use of CXFRS which requires a bean to be setup, which in turn requires a 'serviceClass' as far as I understand. Obviously with the API being a third-party external service there is no way to supply that.

Does anyone have any ideas or directions that they can point me in to merely invoke an external REST API which returns a JSON response?

Much appreciated.


Solution

  • Ok, it turns out I was utterly confused!

    @Component
    

    public class WeatherRESTRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("timer:aTimer?fixedRate=true&period=10s")
                .setHeader(Exchange.HTTP_METHOD, constant("GET"))
                .to("ahc:https://restcountries.p.mashape.com/callingcode/90")
                .routeId("TEST")
                .log("${body}");
    }
    

    This is the working route as per my question and troubles I had the REST API URL in the .from which in Camel land means I want to expose that as a REST Endpoint rather than invoke it.

    I was able to come to my sense while reading through the mailing list linked below.

    http://camel.465427.n5.nabble.com/Making-Periodic-HTTP-Request-Using-Timer-td5749336.html

    P.S.Thank you @6ton I have tried the solutions on that page before hand.