java - 尝试通过Tomcat内部的 Camel 调用外部HTTP Post请求

标签 java http tomcat routes apache-camel

我是 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) {...}

现在,我想通过 camel 路由执行这个 httpPost 请求。任何人都可以帮助我引导我朝着正确的方向前进吗?比如我如何更改上面的 http 代码以通过 Camel 路由。

我试过用

camel/sxx-search

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

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

点击之后

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

应用程序抛出 404 错误。

最佳答案

您是否尝试过将 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>

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

关于java - 尝试通过Tomcat内部的 Camel 调用外部HTTP Post请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53963110/

相关文章:

http - 通过 HTTP 优化文件同步

python - 为什么我获取不到这个http_response的状态码?

eclipse - 如何启动eclipse中打包的apache tomcat?

java - 如何使用 JAX - RS 在 Java 中使用 Web 服务从数据库插入数据

maven - 使用maven将Web应用程序部署到tomcat服务器

java - 第一次单击 X 时 JDialog 未关闭

java - Oracle xpath : two select scenarios 的性能

java - 方法链重构

java - 如果我为前一天设置,为什么我的 alarmManager 会立即启动?

http - HAProxy 保持事件 - 1.5-dev21