将SOAP请求发送到远程Web服务并使用apache Camel

时间:2016-07-29 15:52:08

标签: java web-services soap wsdl apache-camel

我正在进行开发,以便向远程Web服务发送SOAP请求并使用apache Camel获取响应。

在这种情况下,我使用cxf-codegen-plugin为下面提到的WSDl成功生成了Client Side wsdl2java代码。

  • 示例WSDL URL:http://www.webservicex.net/stockquote.asmx?WSDL

在做了一些研究之后,我创建了下面的示例代码,将SOAP请求发送到那里定义的Web服务,并使用生成的客户端代码获取apache Camel的响应。

CamelContext context = new DefaultCamelContext();

HttpComponent httpComponent = new HttpComponent();
context.addComponent("http", httpComponent);

ProducerTemplate template = context.createProducerTemplate();

GetQuote getQuote = new GetQuote();
getQuote.setSymbol("test123");

GetQuoteResponse getQuoteResponse = template.requestBody("http://www.webservicex.net/stockquote.asmx",getQuote, GetQuoteResponse.class);

System.out.println(getQuoteResponse);

但它给出了以下错误。

Caused by: org.apache.camel.InvalidPayloadException: No body available of type: java.io.InputStream but has value: net.webservicex.GetQuote@10bdf5e5 of type: net.webservicex.GetQuote on: Message[ID-namal-PC-33172-1469806939935-0-1]. Caused by: No type converter available to convert from type: net.webservicex.GetQuote to the required type: java.io.InputStream with value net.webservicex.GetQuote@10bdf5e5. Exchange[ID-namal-PC-33172-1469806939935-0-2]. Caused by: [org.apache.camel.NoTypeConversionAvailableException - No type converter available to convert from type: net.webservicex.GetQuote to the required type: java.io.InputStream with value net.webservicex.GetQuote@10bdf5e5]

Caused by: org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: net.webservicex.GetQuote to the required type: java.io.InputStream with value net.webservicex.GetQuote@10bdf5e5

我在这里想念什么?数据绑定?或其他什么?我使用cxf生成客户端代码,以便如何使用cxf发送它?

我只想要将SOAP请求发送到远程Web服务并使用apache Camel获取响应。

  • 骆驼版本:2.9.0
  • Java版本:1.7.x / 1.8.x

1 个答案:

答案 0 :(得分:2)

最好使用CXF组件。根据CXF代码的生成方式,您可能只需发送&在示例中接收字符串而不是对象 - 有关详细信息,请参阅How to tell cxf to keep the wrapper types in methods?

以下是您使用CXF的示例。

CamelContext context = new DefaultCamelContext();

CxfComponent cxfComponent = new CxfComponent(context);
CxfEndpoint serviceEndpoint =
    new CxfEndpoint("http://www.webservicex.net/stockquote.asmx", cxfComponent);

// Service class generated by CXF codegen plugin.
serviceEndpoint.setServiceClass(StockQuoteSoap.class);

ProducerTemplate template = context.createProducerTemplate();

// Request and response can be 'bare' or 'wrapped', see the service class.
String getQuoteResponse = template.requestBody(serviceEndpoint, "MSFT", String.class);

System.out.println(getQuoteResponse);