我需要处理Excel文件并将其内容POST到REST服务。我可以通过Apache POI加载Excel行并将其转换为JSON数组。但是,在POST到REST服务时,它因HTTP状态413而失败,因为由于Excel文件中的行数,POST主体太大。
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
答案 0 :(得分:2)
您可以使用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();
}
}