How to send REST requests with parameters in Apache Camel

2.3k Views Asked by At

I am curious how I can manage the HTTP REST requests in Apache Camel?

I need to iterate through the list of IDs and place the particular ID from the list in HTTP GET request. This list I would receive from a database. I need to send request to third party server and manage the response - insert it into DB.

How can I plan the ID parameter in the request within loop?

@Component
    public class EgrRegistryNumbersRoute
          extends RouteBuilder {
        private final JacksonDataFormat format = new ListJacksonDataFormat(NumbersDTO.class);
        @Autowired
        DataSource dataSource;

List myList = List.of(1, 2, 3, 4, 5);
        
        @Override
        public void configure() throws Exception {
            from("timer://foo?repeatCount=1")
                  .noAutoStartup()
                  .setHeader(Exchange.HTTP_METHOD, constant("GET"))
                  .to("http://server/api/v2/getRegNumByState/**{HERE I WANT VALUE FROM THE LIST}**")
                  .unmarshal(format)
                  .routeId("NumbersRoute-route")
                  .log(">>>${body}")
                  .process(new InsertProcessor("table_name", "column"))
                  .to("jdbc:dataSource");
        }
    }

Processor:

public class InsertProcessor
      implements Processor {
    private final String tableName;
    private final String columns;
    
    public InsertProcessor(String tableName, String columns) {
        this.tableName = tableName;
        this.columns = columns;
    }
    
    @Override
    public void process(Exchange exchange) throws Exception {
        List<NumbersDTO> inputList = exchange.getIn().getBody(List.class);
        StringBuilder queryBuilder = new StringBuilder("INSERT INTO " + tableName + "(" + columns + ") values ");
        System.out.println("Input to be persisted : " + inputList);
        
        inputList.forEach(v -> {
            queryBuilder.append("(").append(v.getNgrn()).append("),");
        });
        String insertQuery = queryBuilder.toString().replaceFirst(".$","");
        System.out.println("Insert Query is : " + insertQuery);
        exchange.getIn().setBody(insertQuery);
    }
}
1

There are 1 best solutions below

2
Pasi Österman On BEST ANSWER

You can achieve this using split and by switching to to toD when defining your http producer endpoint. With toD you can use simple language with the URI.

from("direct:queryRestWithURI")
    .setBody(constant(myList))
    .split(body())
        .toD("http://server/api/v2/getRegNumByState/${body}")
        //do stuff
    .end();

If you don't want to use splitter you can use loop as well to loop through the list.

from("direct:queryRestWithURI")
    .routeId("NumbersRoute-route")
    .setBody(constant(myList))
    .loop(simple("${body.size()}"))
        .setHeader("loopItem", simple("${body[${exchangeProperty.CamelLoopIndex}]}"))
        .toD("http://server/api/v2/getRegNumByState/${headers.loopItem}")
        // do stuff
    .end();