在骆驼中设置REST响应主体 [英] Setting REST response body in camel
问题描述
这是我要在骆驼中设置的流程:
Here is the flow I'm trying to setup in Camel:
GET/product/foo-> MULTICAST [HTTP URI 1,HTTP URI 2,HTTP URI 3]-> AGGREGATE->将汇总值返回到HTTP响应正文
GET /product/foo --> MULTICAST [HTTP URI 1, HTTP URI 2, HTTP URI 3] --> AGGREGATE --> return aggregated value to HTTP response body
我已经以这种方式设置了路由,但是在原始GET的响应中我没有得到任何数据.
I've setup the route this way, BUT I get no data back in the response to the original GET.
如何获取聚合器返回的值?
How can I get the value returned by the aggregator ?
@Override
public void configure() throws Exception {
restConfiguration()
.host("localhost")
.port("8081")
.component("jetty");
from("rest:get:product/foo")
.multicast()
.parallelProcessing()
.aggregationStrategy(new ProductPriceAggregator())
.to("direct:prodcutService1")
.to("direct:prodcutService2")
.to("direct:prodcutService3");
from("direct:prodcutService1")
.to("http4:localhost:9090/simple/product/foo?bridgeEndpoint=true")
.to("direct:aggregate");
from("direct:prodcutService2")
.to("http4:localhost:9091/simple/product/foo?bridgeEndpoint=true")
.to("direct:aggregate");
from("direct:prodcutService3")
.to("http4:localhost:9092/simple/product/foo?bridgeEndpoint=true")
.to("direct:aggregate");
from("direct:aggregate")
.log("${body}").;
}
}
这是我的聚合器:
public class ProductPriceAggregator implements AggregationStrategy {
@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
System.out.println("FOO BAR");
double oldPrice = oldExchange.getIn().getBody(Double.class);
double newPrice = newExchange.getIn().getBody(Double.class);
double finalPrice = oldPrice > newPrice ? newPrice : oldPrice;
oldExchange.getIn().setBody(finalPrice);
return oldExchange;
}
}
推荐答案
这可以做到.
from("direct:aggregate").transform().body();
但是您的汇总策略存在一个小错误.在这里重写了.
But there is a small mistake in your aggregation strategy. Have rewritten it here.
public class ProductPriceAggregator implements AggregationStrategy {
@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange)
{
if (oldExchange == null) {
// the first time we aggregate we only have the new exchange,
// so we just return it
return newExchange;
}
System.out.println("FOO BAR");
double oldPrice = oldExchange.getIn().getBody(Double.class);
double newPrice = newExchange.getIn().getBody(Double.class);
double finalPrice = oldPrice > newPrice ? newPrice : oldPrice;
oldExchange.getIn().setBody(finalPrice);
return oldExchange;
}
}
对于第一次迭代,oldExchange
将为null,因此您需要进行检查并返回newExchange
.
For the first iteration oldExchange
will be null so you need have a check and return newExchange
.
出于某些奇怪的原因(或者可能是这样设计的),骆驼对待Double值的方式完全不同.要使其正常工作,请进行以下更改.
For some strange reason(or may be it is designed like this) Camel is treating Double values completely different. To make it work, do the following changes.
from("rest:get:product/foo")
.setHeader("Accept", simple("application/json"))
.multicast()
.parallelProcessing()
.......
这是因为,默认情况下,它将text/html作为接受类型,并且像html标签(如<Double>2.345<Double>
)一样出现了双精度值.因此,您需要将类型指定为application/json以进行更好的处理.
This is because, by default it takes text/html as Accept type and double values are coming like html tags like <Double>2.345<Double>
. So you need to specify type as application/json for better processing.
在Aggregator代码中,您需要这样做.
And in the Aggregator code you need to do like this.
double oldPrice = Double.valueOf(oldExchange.getIn().getBody(String.class));
double newPrice = Double.valueOf(newExchange.getIn().getBody(String.class));
double finalPrice = oldPrice > newPrice ? newPrice : oldPrice;
oldExchange.getIn().setBody(Double.toString(finalPrice));
这篇关于在骆驼中设置REST响应主体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!