我的要求是有一个代理网络服务,它将接收来自客户端的请求,然后通过骆驼路由,丰富它并将其转发到其他客户端位置的真实网络服务,获得响应并将其发送回原始请求者。
我基本上看了骆驼分布中的camel-cxf-proxy示例(http://camel.apache.org/cxf-proxy-example.html)和camel-cxf-example。它与camel-cxf-proxy完全相似并且出现了这条路线
<from uri="cxf:bean:soapMessageEndpoint?dataFormat=MESSAGE" />
<camel:convertBodyTo type="java.lang.String"></camel:convertBodyTo>
<to ref="XService"></to>
端点在哪里
<endpoint id="XService" uri="http://xx.xx.xxx.xx:8080/X_Service" />
<cxf:cxfEndpoint id="soapMessageEndpoint" address="http://localhost:8181/ProviderInformationDirectoryService" wsdlURL="wsdl/HPD_ProviderInformationDirectory.wsdl" endpointName="s:ProviderInformationDirectory_Port_Soap" serviceName="s:ProviderInformationDirectory_Service" xmlns:s="urn:ihe:iti:hpd:2010"/>
如您所见,第二个服务是http端点。首先是camel-cxf代理。我只有wsdl,此时不需要impl。数据格式是MESSAGE,因为我需要发送整个soap信封到第二个 Web 服务,并且客户端的请求中有一些有用的标头。但是当我使用示例肥皂信封运行这条路线时,它总是会出现 500 响应。我认为发送到真实 Web 服务的消息不是它所期望的。
我尝试跟踪骆驼路线,但没有显示太多。我希望它会显示对 http 端点的真实请求。我尝试配置拦截器,但也没有用。跟踪只显示以下
Failed delivery for (MessageId: ID-ALHCAN0437-63941-1354828653539-45-2 on ExchangeId: ID-ALHCAN0437-63941-1354828653539-45-1). Exhausted after delivery attempt: 1 caught: org.apache.camel.component.http.HttpOperationFailedException: HTTP operation failed invoking http://X:8080/X_Service with statusCode: 500
我还尝试了以下似乎有效的方法。
<from uri="cxf:bean:soapMessageEndpoint?dataFormat=MESSAGE" />
<camel:convertBodyTo type="java.lang.String"></camel:convertBodyTo>
<to uri="bean:callRemoteWS"></to>
callRemoteWS (callRemoteMethod) 将soapenvelope 作为字符串获取,并向上面的端点发出HTTPPost 请求,返回响应。
public String callRemoteMethod(String request) throws Exception{
HttpClient client = new HttpClient();
BufferedReader br = null;
PostMethod method = new PostMethod("http://x.x.x.x:8080/X_Service");
RequestEntity entity =new StringRequestEntity(request);
method.setRequestEntity(entity);
try{
int returnCode = client.executeMethod(method);
if (returnCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
return "Error";
}
// Read the response body.
byte[] responseBody = method.getResponseBody();
System.out.println(new String(responseBody));
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
return new String(responseBody);
} finally {
method.releaseConnection();
if(br != null) try { br.close(); } catch (Exception fe) {}
}
}
我很困惑为什么带有http webservice的简单camel-cxf代理不起作用而第二个起作用(它应该并且确实有效:P)。我的代码是否正常。对我来说似乎不对。我很确定一些交流属性设置错误或发送到真实Web服务的内容错误。从代理获取的内容用于在第二个路由中进行Httppost调用。因此从代理获取的内容不能错误。当它从交换获取并发送到真实Web服务时出了点问题。任何人都可以对此有所了解。