【问题标题】:Trying to call external HTTP Post request through camel inside Tomcat试图通过Tomcat内部的骆驼调用外部HTTP Post请求
【发布时间】:2018-12-29 04:48:43
【问题描述】:

我是 Apache Camel 的新手,能够成功设置初始 hello servlet。我在 Tomcat 容器中使用 Apache Camel。

下面是我的camel-config.xml

  <camelContext xmlns="http://camel.apache.org/schema/spring">

<route id="helloRoute">
  <!-- incoming requests from the servlet is routed -->
  <from uri="servlet:hello"/>
  <choice>
    <when>
      <!-- is there a header with the key name? -->
      <header>name</header>
      <!-- yes so return back a message to the user -->
      <transform>
        <simple>Hi I am ${sysenv.HOSTNAME}. Hello ${header.name} how are you today?</simple>
      </transform>
    </when>
    <otherwise>
      <!-- if no name parameter then output a syntax to the user -->
      <transform>
        <constant>Add a name parameter to uri, eg ?name=foo</constant>
      </transform>
    </otherwise>
  </choice>
</route>
<route id="svRoute">
    <from uri="servlet:camel/sxx-search"/>
    <to uri="https4://sxx.abc.com/sxx/sxx.php"/>
</route>
</camelContext>

这是来自我的 web.xml 的代码

<servlet>
        <servlet-name>CamelServlet</servlet-name>
        <servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class>
      </servlet>

      <!-- Camel servlet mapping -->
      <servlet-mapping>
        <servlet-name>CamelServlet</servlet-name>
        <url-pattern>/camel/*</url-pattern>
</servlet-mapping>

当我击球时 http://localhost:port/camel/hello?name=Mr 我得到了想要的响应。

目前,在下面的代码中,我使用 XML 作为发布请求来访问外部 HTTP URL,并作为响应接收 XML(String)

String xml="<some input to the httppost>"
HttpPost httpPost = new HttpPost("camel/sxx-search"); //tryin to map this in route defined in config.xml above
 SSLContext sslctx= SSLContexts.createSystemDefault();

    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
            sslctx,
            new String[] { "TLSv1", "TLSv1.1", "TLSv1.2" },
            null,
            SSLConnectionSocketFactory.getDefaultHostnameVerifier());

    CloseableHttpClient httpclient = HttpClients.custom()
            .setSSLSocketFactory(sslsf)
            .addInterceptorFirst(new RequestAcceptEncoding())   // adds gzip encoding header
            .build();

    CloseableHttpResponse response = null;
    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("xml", xml)); // URLEncoding taken care of in the next line

    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
        response = httpclient.execute(httpPost);
        result = EntityUtils.toString(response.getEntity(), "UTF-8"); } catch(Exception e) {...}

现在,我想通过骆驼路线执行这个 httpPost 请求。任何人都可以帮助我引导我朝着正确的方向前进吗?就像我怎样才能改变上面的http代码来通过骆驼路线。

我尝试过使用

camel/sxx-search

在上面写的 HttpPost 中。这样它就可以通过骆驼路由到

"https4://sxx.abc.com/sxx/sxx.php" 

点击后

http://localhost:8080/camel/sxx-search

应用程序抛出 404 错误。

【问题讨论】:

    标签: java http tomcat routes apache-camel


    【解决方案1】:

    您是否尝试过将 http 方法设置为 POST?

    camel docs 有如何在邮件头中设置它的示例。例如

      <route>
        <from uri="direct:start"/>
        <setHeader headerName="CamelHttpMethod">
            <constant>POST</constant>
        </setHeader>
        <to uri="http4://www.google.com"/>
        <to uri="mock:results"/>
      </route>
    

    我假设您还需要设置要在消息正文中发布的数据。

    【讨论】:

    • 我已经在使用 HttpPost API,我想使用 HttpPost API 发布到 Apache Camel uri。这可能吗?
    猜你喜欢
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多