1

我正在使用以下 SOAP 客户端在 Apache OFBiz 中创建 Web 服务:

public class CreatePerson {

private static OMFactory fac;
private static OMNamespace omNs;
public static String fname;
public static String lname;

static {
      fac = OMAbstractFactory.getOMFactory();
      omNs = fac.createOMNamespace("http://my-ip-adress/service/", "ns1");
}

public static void main(String[] args) throws AxisFault {

      ServiceClient sc = new ServiceClient();
      Options opts = new Options();
      opts.setTo(new EndpointReference("http://my-ip-adress:port/webtools/control/SOAPService"));
      opts.setAction("createPerson");
      sc.setOptions(opts);
      OMElement res = sc.sendReceive(createPayLoad(fname, lname));
      System.out.println(res);
}

public static OMElement createPayLoad(@XPath("//person/return[1]")String firstName, @XPath("//person/return[2]")String lastName) {

      CreatePerson.fname = firstName;
      CreatePerson.lname = lastName;

      OMElement createPerson = fac.createOMElement("createPerson", omNs);
      OMElement mapMap = fac.createOMElement("map-Map", omNs);

      createPerson.addChild(mapMap);

      mapMap.addChild(createMapEntry("login.username", "admin"));
      mapMap.addChild(createMapEntry("login.password", "ofbiz"));
      // do the mapping here!
      mapMap.addChild(createMapEntry("firstName", firstName));
      mapMap.addChild(createMapEntry("lastName", lastName));

      return createPerson;
}

public static OMElement createMapEntry(String key, String val) {

      OMElement mapEntry = fac.createOMElement("map-Entry", omNs);

      // create the key
      OMElement mapKey = fac.createOMElement("map-Key", omNs);
      OMElement keyElement = fac.createOMElement("std-String", omNs);
      OMAttribute keyAttribute = fac.createOMAttribute("value", null, key);

      mapKey.addChild(keyElement);
      keyElement.addAttribute(keyAttribute);

      // create the value
      OMElement mapValue = fac.createOMElement("map-Value", omNs);
      OMElement valElement = fac.createOMElement("std-String", omNs);
      OMAttribute valAttribute = fac.createOMAttribute("value", null, val);

      mapValue.addChild(valElement);
      valElement.addAttribute(valAttribute);

      // attach to map-Entry
      mapEntry.addChild(mapKey);
      mapEntry.addChild(mapValue);

      return mapEntry;
}
}

接下来,我想使用以下 xml 在上面的客户端中使用 firstName 和 lastName 将 return-element 中的值获取到“map”(AnnotatedEntryPointResolver):

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ns2:getAllResponse xmlns:ns2="http://service.ofbiz.org/">
      <person>
        <return>Testname</return>
        <return>Test</return>
      </person>
    </ns2:getAllResponse>
  </soap:Body>
</soap:Envelope>

因此,我使用的是骡子。正如您在我的客户端代码中看到的那样,我添加了一些 XPath 注释来引用返回元素中的 xml 值。出于测试目的,我的 Mule 配置很简单:

<flow name="test_flow" doc:name="test_flow">
    <file:inbound-endpoint path="[mypath]\xml\in" responseTimeout="10000" doc:name="File"/>
    <component class="org.ofbiz.service.CreatePerson" doc:name="Java"/>
</flow>

我只是使用文件入站端点和上面引用我的客户端的 java 组件。运行 Mule 后,在我的客户端的 createPayLoad() 方法中正确完成了“映射”。但这不是我想做的。我的问题:对于本示例,如何使用 AnnotatedEntryPointResolvers 调用整个 java 组件(包括主方法)?如上所述,是否有替代或更好的解决方案?

4

1 回答 1

1

调用main()很奇怪,但是......你可以这样做:

<scripting:component>
    <scripting:script engine="groovy">CreatePerson.main([] as String[])</scripting:script>
</scripting:component>
于 2013-02-08T17:16:25.110 回答