我需要处理Excel文件并将其内容发布到REST服务。我可以通过Apache POI加载Excel行并将其转换为JSON数组。但是,在POST到REST服务时,它失败并显示HTTP状态413,因为POST正文由于Excel文件中的行数太大而变得太大。
Apache Camel中是否有一种方法可以限制REST服务的JSON输入大小并重复调用REST服务调用。请帮忙。
以下是Java DSL路由配置。
from("file:/excelfilelocation/inputexcel.xls")
.bean(new ExcelConverter(), "processExcel") // Converts excel rows to ArrayList of model object
.marshal().json(JsonLibrary.Jackson)
.setHeader("Authorization", simple(apiKEY))
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.setHeader(Exchange.HTTP_URI, simple(API_URL))
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
.to(API_URL)
.to("mock:finish");
错误是由于POST正文内容长度过长而导致的。
org.apache.camel.http.common.HttpOperationFailedException:HTTP
操作调用失败
https://test.com/api/v2/receipts,带有statusCode:
413
最佳答案
您可以使用Splitter EIP拆分Excel条目,然后使用Aggregator EIP将它们收集到更易于管理的批次中,然后发送这些批次:
from("file:/excelfilelocation?fileName=inputexcel.xls")
.bean(new ExcelConverter(), "processExcel")
.split(body())
.aggregate(constant(true), new GroupedBodyAggregationStrategy())
.completionSize(100)
.completionTimeout(1000)
.marshal().json(JsonLibrary.Jackson)
.setHeader("Authorization", simple(apiKEY))
.setHeader(Exchange.HTTP_METHOD, constant("POST"))
.setHeader(Exchange.HTTP_URI, simple(API_URL))
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
.to(API_URL);
这是将行收集到列表中的聚合策略:
public class GroupedBodyAggregationStrategy extends AbstractListAggregationStrategy<Message> {
@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
if (oldExchange == null) {
// for the first time we must create a new empty exchange as the
// holder, as the outgoing exchange
// must not be one of the grouped exchanges, as that causes a
// endless circular reference
oldExchange = new DefaultExchange(newExchange);
}
return super.aggregate(oldExchange, newExchange);
}
@Override
public Object getValue(Exchange exchange) {
return exchange.getIn().getBody();
}
}
关于java - Apache Camel-拆分JSON数组并迭代地向REST服务调用POST,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38946437/