Apache camel 使用驼峰http处理CachedOutStream

Apache camel 使用驼峰http处理CachedOutStream,apache-camel,camel-http,Apache Camel,Camel Http,嗨~我正在使用camel http组件。我无法提取身体信息 这是我的密码 .log(LoggingLevel.INFO, "ToUri ===> ${body}") .toD("${body}") .log(LoggingLevel.INFO, "Result ===> ${body}") .process(new Processor() { public void process(Exchange exchange) throws Exception {

嗨~我正在使用camel http组件。我无法提取身体信息

这是我的密码

.log(LoggingLevel.INFO, "ToUri ===> ${body}")
.toD("${body}")
.log(LoggingLevel.INFO, "Result ===> ${body}")
.process(new Processor() {


    public void process(Exchange exchange) throws Exception {

        long startTime = System.currentTimeMillis();
        Message inboundMessage = exchange.getIn();


        Object body = exchange.getIn().getBody();
        String msg = inboundMessage.getBody(String.class);

        System.out.println("body:"+body);
        System.out.println("getInBody msg:"+msg);
        System.out.println("getInBody body:"+body.toString());
    =======================================================================         
body : org.apache.camel.converter.stream.CachedOutputStream$WrappedInputStream@28936ba4
getInBody msg:
getInBody bodybodybody:org.apache.camel.converter.stream.CachedOutputStream$WrappedInputStream@28936ba4
这原木很好用。像这样

09:56:53.523 INFO  route1 - ToUri ===> https://translation.googleapis.com/language/translate/v2?key=tesetKey&source=en&target=ja&q=hi
09:56:54.545 INFO  route1 - Result ===> {
  "data": {
    "translations": [
      {
        "translatedText": "こんにちは"
      }
    ]
  }
}
我想使用camel提取翻译文本

我如何处理CachedOutStream,这是什么

我在查骆驼医生,不明白。请给我一个提示来解决我的问题


谢谢。

有关缓存输出流的信息,请参见流缓存:

要从处理器获取字符串形式的消息体,只需执行以下操作

 String body = exchange.getIn().getBody(String.class);
这将告诉Camel您希望消息作为
字符串
,它将自动将消息正文从
CachedOutputStream
转换为
字符串
。然后,您可以通过常规Java代码获取所需的文本


还要注意的是,您可以使用jsonpath处理json数据并获取信息,但是它的语法可能需要学习一点:

在调用processor之前,您已经获取了流数据(使用
.log
)。流数据只能提取一次。尝试删除日志步骤,您可以进入处理器:

.log(LoggingLevel.INFO, "Result ===> ${body}")
.process(new Processor() {

在花了2天的时间进行实验后,您可以使用convertBodyTo(类类型)方法来实现这一点,如下所示

09:56:53.523 INFO  route1 - ToUri ===> https://translation.googleapis.com/language/translate/v2?key=tesetKey&source=en&target=ja&q=hi
09:56:54.545 INFO  route1 - Result ===> {
  "data": {
    "translations": [
      {
        "translatedText": "こんにちは"
      }
    ]
  }
}
.log(LoggingLevel.INFO,“结果==>${body}”)

.convertBodyTo(String.class)


.process(new Processor(){…}

我试过像这样的字符串body=exchange.getIn().getBody(String.class);System.out.println(“#process bod:“+body”);但它返回空值…为什么?在Camel 2.0中,流缓存是默认禁用的开箱即用…我如何启用流缓存?很抱歉打扰您。。。