在驼峰中设置REST响应体

时间:2018-02-02 20:10:44

标签: java apache-camel

以下是我尝试在Camel中设置的流程:

GET / product / foo - > MULTICAST [HTTP URI 1,HTTP URI 2,HTTP URI 3] - > AGGREGATE - >将聚合值返回给HTTP响应正文

我用这种方式设置了路由,但是在原始GET的响应中我没有得到任何数据。

如何获取聚合器返回的值?

 @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;
    }
}

1 个答案:

答案 0 :(得分:1)

这样做。

from("direct:aggregate").transform().body(); 

但是您的聚合策略存在一个小错误。在这里重写了它。

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

修改

由于一些奇怪的原因(或者可能是这样设计),Camel正在将Double值视为完全不同。要使其工作,请执行以下更改。

 from("rest:get:product/foo")
                .setHeader("Accept", simple("application/json"))
                .multicast()
                .parallelProcessing()
                .......

这是因为默认情况下它将text / html作为Accept类型,而double值就像<Double>2.345<Double>之类的html标签。因此,您需要将类型指定为application / json以便更好地处理。

在Aggregator代码中你需要这样做。

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));