向远程 Web 服务发送 SOAP 请求并使用 apache Camel 获取响应 [英] Send a SOAP request to a remote web service and get a response using apache Camel

查看:32
本文介绍了向远程 Web 服务发送 SOAP 请求并使用 apache Camel 获取响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发向远程 Web 服务发送 SOAP 请求并使用 apache Camel 获得响应.

I'm doing a development to send a SOAP request to a remote web service and get a response using apache Camel.

在这种情况下,我使用 cxf-codegen-plugin 为下面提到的 WSDl 成功生成了客户端 wsdl2java 代码.

In this case, I succesfully generated Client Side wsdl2java Code using the cxf-codegen-plugin for the WSDl mentioned below.

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

在做了一些研究之后,我创建了下面的示例代码,以向其中定义的 Web 服务发送 SOAP 请求,并使用生成的客户端代码通过 apache Camel 获得响应.

And after doing some researches, I created below sample code to send a SOAP request to the web service defined in there and get a response with apache Camel using the generated client code.

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

推荐答案

为此最好使用 CXF 组件.根据 CXF 代码的生成方式,您可能只发送 &在您的示例中接收字符串而不是对象 - 请参阅 如何告诉 cxf 在方法中保留包装器类型? 了解更多信息.

It would be better to use the CXF component for this. Depending on how the CXF code is generated you might just send & receive a string instead of an object in your example - see How to tell cxf to keep the wrapper types in methods? for more information.

这是您使用 CXF 的示例.

Here's your example with 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);

这篇关于向远程 Web 服务发送 SOAP 请求并使用 apache Camel 获取响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆