使用 apache Camel 向远程 Web 服务发送 SOAP 请求并获得响应
Send a SOAP request to a remote web service and get a response using apache Camel
我正在进行开发以向远程 Web 服务发送 SOAP
请求并使用 apache Camel
获得响应。
在这种情况下,我使用下面提到的 WSDl 的 cxf-codegen-plugin
成功生成了客户端 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 发送此代码?
我只想向远程 Web 服务发送 SOAP 请求并使用 apache Camel 获得响应。
- 骆驼版本:2.9.0
- Java 版本:1.7.x / 1.8.x
最好使用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);
我正在进行开发以向远程 Web 服务发送 SOAP
请求并使用 apache Camel
获得响应。
在这种情况下,我使用下面提到的 WSDl 的 cxf-codegen-plugin
成功生成了客户端 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 发送此代码?
我只想向远程 Web 服务发送 SOAP 请求并使用 apache Camel 获得响应。
- 骆驼版本:2.9.0
- Java 版本:1.7.x / 1.8.x
最好使用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);