Mule - 如何发送SOAP Web服务请求?

时间:2014-01-10 09:35:43

标签: web service mule

嗨我正在骡子做简单的POC。

我有一个网络服务,我想让它成为客户端。

它是SOAP Web服务,我想向它发送请求,但我没有得到关注。请给我一些想法。

以下是代码:

MULE:

<cxf:configuration name="CXF_Configuration" enableMuleSoapHeaders="true" initializeStaticBusInstance="true" doc:name="CXF Configuration"/>

<flow name="prjvm1" doc:name="prjvm1">
    <http:inbound-endpoint address="http://localhost:5678/httpHello" contentType="application/x-www-form-urlencoded" doc:name="HTTP">
        <http:body-to-parameter-map-transformer />
    </http:inbound-endpoint>

    <!-- This logger is just set to show the message accepted from the request -->
    <logger level="INFO" message="#[payload]" doc:name="Logger"/>
        <cxf:jaxws-client doc:name="VimService"    
                      wsdlLocation="file:/C:/Users/gugla/MuleStudio/workspace/prjvm/bin/service/vService.wsdl" 
                      operation="retrieveServiceContent"  
                      clientClass="com.esxclient.VService" 
                      port="VimPort">
        <cxf:jaxb-databinding/>
    </cxf:jaxws-client>
    <outbound-endpoint address="http://localhost:8080/gep-sped/servicos/ServicoDeCadastroEAgendamento" 
                        doc:name="Generic" 
                        exchange-pattern="request-response"/>
    <echo-component doc:name="Echo"/>
</flow>

我遇到了异常,但是在WSDL中有操作

Message               : No such operation: retrieveServiceContent. Failed to route event via endpoint: org.mule.module.cxf.CxfOutboundMessageProcessor. Message payload is of type: ManagedObjectReference
Code                  : MULE_ERROR--2

--------------------------------------------------------------------------------
Exception stack is:
1. No such operation: retrieveServiceContent (java.lang.Exception)
  org.mule.module.cxf.CxfOutboundMessageProcessor:282 (null)
2. No such operation: retrieveServiceContent. Failed to route event via endpoint: org.mule.module.cxf.CxfOutboundMessageProcessor. Message payload is of type: ManagedObjectReference (org.mule.api.transport.DispatchException)
  org.mule.module.cxf.CxfOutboundMessageProcessor:150 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transport/DispatchException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
java.lang.Exception: No such operation: retrieveServiceContent
    at org.mule.module.cxf.CxfOutboundMessageProcessor.getOperation(CxfOutboundMessageProcessor.java:282)
    at org.mule.module.cxf.CxfOutboundMessageProcessor.getMethodFromOperation(CxfOutboundMessageProcessor.java:322)
    at org.mule.module.cxf.CxfOutboundMessageProcessor.getMethod(CxfOutboundMessageProcessor.java:259)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)

以下是运行中的WSDL:我很困惑,大约4天就可以了,因为我是骡子的新手。

  <operation name="RetrieveServiceContent">
     <input message="vim2:RetrieveServiceContentRequestMsg" />
     <output message="vim2:RetrieveServiceContentResponseMsg" />
     <fault name="RuntimeFault" message="vim2:RuntimeFaultFaultMsg"/>
  </operation>

1 个答案:

答案 0 :(得分:1)

有几种方法可以做到这一点。我更喜欢MuleStudio希望你这样做的方式,因为我从来没有真正开始工作。基本上,每当我创建一个webservice客户端时,我的mule-config看起来像这样:

<custom-transformer class="nl.thorax.someprogram.transformers.SomeRequestTransformer
<https:outbound-endpoint ref="someEndpoint" >
   <cxf:jaxws-client 
        clientClass="nl.thorax.someprogram.someclass"
        wsdlLocation="http://somedomain?wsdl
        port="somePort" 
        operation="someOperation"/>
</https:outbound-endpoint>
<custom-transformer class="nl.thorax.someprogram.transformers.SomeResponseTransformer

其中:

  • SomeRequestTransformer:用于创建请求的转换器。
  • someEndpoint:MuleConfig中定义的某个端点。在我的例子中,这是https,但它也可以是普通的http。
  • someclass:重要!这是您的客户端类。它可以通过Apache的WSDL2Java等免费工具从WSDL生成。谷歌是你的朋友。
  • somePort:您要使用的Web服务中的端口。它通常可以在WSDL本身或您生成的客户端类中找到。端口或多或少指定了您可以使用的操作。
  • someOperation:您要使用的操作。确保它的输入与客户端类中的定义完全相同。错误使用cApItAlS会导致错误!
  • SomeResponseTransformer:用你的回复做某事的转换器。

现在,我配置调用的方式(并解析响应)是使用POJO。我的第一个变换器SomeRequestTransformer(基于Mule库中的AbstractMessageTransformer)有一些代码如下:

public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
{
  RequestObject request = new RequestObject();
  request.setText("Hello!");
  message.setPayload(request);
  return message;
}

我创建请求,设置变量并将其返回给Mule。 RequestObject是由WSDL2Java生成的类,对应于WSDL中的某些操作。解析响应的方式大致相同。

现在我从经验中知道很多网络服务都没有完全以相同的方式工作。尝试自己实现我的例子。如果这不起作用,请提供您的Mule-Config以及您可能正在使用的任何和所有Java类。

修改 我创建了一个实际可行的方法示例。这些文件可以在our website下载。请参阅文件中的注释。当然,您必须手动创建Mule项目。

示例的注意事项:

  • Mule-config的XML表示可以在档案的'resources'文件夹中找到。
  • 'nl.example.example'文件夹包含所有生成的JAX-WS文件。
  • Mule-config中的WSDL位置必须更改,因为它包含绝对路径。
  • 该示例在http://localhost:8088处创建一个端点供您调用。该流包含一个变换器,它使用预定义的参数创建一个示例调用。然后它尝试连接到Web服务。我在创建模拟服务时使用了SOAPUI使用的默认地址,但当然可以将其更改为您想要的任何内容。 webservice(据说)返回一些回显到用户浏览器中的东西。
  • 在此示例中,参数实际上是字符串,因为WSDL请求不包含任何内容。要确定要传递给cxf:jaxws-client的对象,请查看可在生成文件的端口定义中找到的操作定义。