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);
}
}
You can achieve this using split and by switching
tototoDwhen defining your http producer endpoint. WithtoDyou can use simple language with the URI.If you don't want to use splitter you can use loop as well to loop through the list.