【问题标题】:Call REST service from Apache Camel Blueprint从 Apache Camel Blueprint 调用 REST 服务
【发布时间】:2016-10-04 05:32:13
【问题描述】:

我想调用返回 JSON 的外部 REST 服务,REST 本身具有基本身份验证(我不知道如何发送基本身份验证),我已经阅读了一些教程以及引导我的 CXFRS 组件到这个blueprint.xml

<?xml version="1.0" encoding="UTF-8"?>

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cxf="http://camel.apache.org/schema/cxf"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xsi:schemaLocation="
       http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
       http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd
       http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">

  <bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.json.JSONProvider"/>

  <cxf:rsClient id="rsClient" address="http://localhost/test.php">
    <cxf:providers>
      <ref bean="jsonProvider"/>
    </cxf:providers>
  </cxf:rsClient>

  <camelContext xmlns="http://camel.apache.org/schema/blueprint">
    <propertyPlaceholder location="classpath:sql.properties" id="placeholder"/>

    <route>
      <from uri="cxfrs://bean://rsClient"/>
      <log message="${body}"/>
    </route>

  </camelContext>

</blueprint>

如果它甚至是相关的,我正在使用 JBoss Developer Studio 进行开发和测试,使用右键单击 blueprint.xml -&gt; Run As -&gt; Local Camel Context (without tests) 这里是输出。

[ERROR] *************************************
[ERROR] Error occurred while running main from: org.apache.camel.test.blueprint.Main
[ERROR] 
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.camel.maven.RunMojo$1.run(RunMojo.java:488)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: Gave up waiting for service (objectClass=org.apache.camel.CamelContext)
    at org.apache.camel.test.blueprint.CamelBlueprintHelper.getOsgiService(CamelBlueprintHelper.java:265)
    at org.apache.camel.test.blueprint.CamelBlueprintHelper.getOsgiService(CamelBlueprintHelper.java:226)
    at org.apache.camel.test.blueprint.Main.doStart(Main.java:110)
    at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
    at org.apache.camel.main.MainSupport.run(MainSupport.java:150)
    at org.apache.camel.main.MainSupport.run(MainSupport.java:354)
    at org.apache.camel.test.blueprint.Main.main(Main.java:84)
    ... 6 more
[ERROR] *************************************
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:07 min
[INFO] Finished at: 2016-10-04T12:11:28+07:00
[INFO] Final Memory: 30M/314M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.camel:camel-maven-plugin:2.15.1.redhat-621084:run (default-cli) on project dsource: null: MojoExecutionException: InvocationTargetException: Gave up waiting for service (objectClass=org.apache.camel.CamelContext) -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

除了我得到的错误,我究竟如何使用 Blueprint 从 Apache Camel 调用或调用 REST 服务并获取返回的 JSON?

【问题讨论】:

  • 我认为使用 rest dsl 来进行简单的休息操作要容易得多。见这里:camel.apache.org/rest-dsl.html。另外,您使用红帽版本的任何原因?
  • Rest DSL 实际上只是为了定义或创建 REST 服务器服务吗?不使用或调用外部 REST 服务?因为我工作的公司把 RedHat JBoss 作为我们集成的要求。哦对了,看Rest DSL的时候有camel-jetty,可以用吗?

标签: json rest apache-camel blueprint-osgi cxfrs


【解决方案1】:

我解决了这个问题,但是使用 Camel HTTP 组件,我可以轻松调用 REST API 并获取 JSON 响应,但首先我必须先设置一些标头。

<route>
  <from uri="timer:foo?repeatCount=1"/>

  <setHeader headerName="Exchange.HTTP_METHOD">
    <constant>GET</constant>
  </setHeader>

  <to uri="http:localhost/api/test"/>
  <log message="${body}"/>
</route>

【讨论】:

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