【问题标题】:apache camel - call an external web serviceapache camel - 调用外部网络服务
【发布时间】:2014-01-09 03:20:25
【问题描述】:

我想向需要调用的外部 Web 服务发送 http 帖子。

<from uri="cxfrs://http://localhost:9876?resourceClasses=MyResource"/>
            <log message="Received. " loggingLevel="INFO" logName="MyLogger"/>
            <setHeader headerName="CamelHttpMethod">
                 <constant>POST</constant>
            </setHeader>
            <setHeader headerName="Content-Type">
                <constant>application/json</constant>
            </setHeader>
            <setBody>
                <simple>param1=param1value&amp;param2=param2value</simple>
            </setBody>
<to uri="http://samplesample.com?bridgeEndpoint=true" />
<log message="body is ${body}" loggingLevel="INFO" logName="MyLogger"/>

我遇到了一个异常:

Caused by: org.apache.camel.component.http.HttpOperationFailedException: HTTP operation failed invoking http://samplesample.com with statusCode: 400

当我使用 rest 客户端启动请求时,它工作正常。任何帮助将不胜感激。

【问题讨论】:

  • 根据错误,您没有发送正确的语法。下载wireshark对rest客户端调用和camel调用进行数据包嗅探并进行比较。我确信这两个调用是不同的,您需要找出原因。

标签: web-services rest exception apache-camel


【解决方案1】:

camel-cxfrs 消费者会将 REST 请求转换为方法调用,因此 camel-http 生产者可能无法正确解释消息。

如果你想使用camel代理REST请求,你可以使用camel-jetty组件来做。

from("jetty://http://localhost:9876?matchOnUriPrefix=true")
                    .to("http://samplesample.com?throwExceptionOnFailure=false&bridgeEndpoint=true");

【讨论】:

    【解决方案2】:

    您应该设置适当的标头,而不是将您的请求参数放入消息正文,例如用于添加 HTTP 查询参数使用

    <setHeader headerName="CamelHttpQuery">
     <constant>param1=param1value&amp;param2=param2value</constant>
    </setHeader>
    

    或用于添加 HTTP 路径参数使用

    <setHeader headerName="CamelHttpPath">
     <constant>/param1/20</constant>
    </setHeader>
    

    【讨论】:

      猜你喜欢
      • 2015-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-06
      相关资源
      最近更新 更多