【问题标题】:Send a SOAP request to a remote web service and get a response using apache Camel向远程 Web 服务发送 SOAP 请求并使用 apache Camel 获得响应
【发布时间】:2016-07-29 15:52:08
【问题描述】:

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

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

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

在做了一些研究之后,我创建了下面的示例代码,以向其中定义的 Web 服务发送 SOAP 请求,并使用生成的客户端代码通过 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

【问题讨论】:

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


    【解决方案1】:

    最好使用 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);
    

    【讨论】:

    • 非常感谢bgossit!它为此工作正常。我有一个问题。如果需要多个参数,如何发送请求正文。例如:如何为下面的调用(SessionCreateRS sessionCreateRS = sessionCreatePortType.sessionCreateRQ(messageHeader,securityHeader,sessionCreateRQ)发送请求参数(messageHeader,securityHeader,sessionCreateRQ);//这是来自JAX-WS)
    • 哦,我有一个愚蠢的答案。我只是将它们添加到列表中并发送。有效!请分享任何更好的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多