1
votes

Hi I want to invoke a rest service whose URL is

http://ex.abc.com/orders/resources/{var1}/{var2}/details?orderNumber=XXXXX

where var1 and var2 are dynamic values. Based on the input they will change. I also want to set 2 headers say key1:value1 , key2:value2.

How can I make a rest call to the given url with given headers and then see the response using Apache Camel? (The response will always be JSON).

4

4 Answers

0
votes

You can make use of a dynamic uri in your route block. See http://camel.apache.org/how-do-i-use-dynamic-uri-in-to.html Note that this can be done in both from() and to().

Example:
[From (previous application endpoint)]
-> [To (perform rest with dynamic values from the exchange)]
-> [To (process the returned json)]

0
votes

If you're making a rest call you can use the CXFRS component. At the very bottom of the page you'll see an example of a processor that's setting up a message as a rest call. With regards to your question, you can use

inMessage.setHeader(Exchange.HTTP_PATH, "/resources/{var1}/{var2}/details?orderNumber=XXXXX");

to set up any path parameters that you might need.

0
votes

Please Try to use camel servlet.

< from uri="servlet:///orders/resources/{$var1}/{$var2}/details?orderNumber=XXXXX" />

in web.xml, < url-pattern>/ * < /url-pattern>

Reference: http://camel.apache.org/servlet.html

0
votes

Below is the example where you can find a Rest call with Apache Camel.

package camelinaction;

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import org.apache.camel.spring.boot.FatJarRouter;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class OrderRoute extends FatJarRouter {

    @Bean(name = "jsonProvider")
    public JacksonJsonProvider jsonProvider() {
        return new JacksonJsonProvider();
    }

    @Override
    public void configure() throws Exception {
        // use CXF-RS to setup the REST web service using the resource class
        // and use the simple binding style which is recommended to use
        from("cxfrs:http://localhost:8080?resourceClasses=camelinaction.RestOrderService&bindingStyle=SimpleConsumer&providers=#jsonProvider")
            // call the route based on the operation invoked on the REST web service
            .toD("direct:${header.operationName}");

        // routes that implement the REST services
        from("direct:createOrder")
            .bean("orderService", "createOrder");

        from("direct:getOrder")
            .bean("orderService", "getOrder(${header.id})");

        from("direct:updateOrder")
            .bean("orderService", "updateOrder");

        from("direct:cancelOrder")
            .bean("orderService", "cancelOrder(${header.id})");
    }
}

Source code link :

https://github.com/camelinaction/camelinaction2/tree/master/chapter10/camel-cxf-rest-spring-boot.

I highly encourage to refer camelinaction2 as it covers many advance topics and it is written by @Claus Ibsen.