0

我编写了下面的小应用程序来列出使用 Apache CXF 库的所有方法和肥皂服务。此应用程序列出了服务的所有方法,但正如您在运行此应用程序时在输出中看到的那样,服务方法的输入参数和返回类型是复杂类型的 JAXBElement。我希望 cxf 不生成 JAXBElement,而是希望在运行时生成其原始类中的复杂类型。正如在http://s141.codeinspot.com/q/1455881上所说,可以通过将 cxf 库的 wsdl2java 实用程序的 generateElementProperty 属性值设置为 false 来完成,但我找不到动态方法调用的相同参数与 cxf 库。我想获取原始类型的输入参数和返回类型。

import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.List;
import org.apache.cxf.binding.Binding;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.service.model.BindingInfo;
import org.apache.cxf.service.model.BindingMessageInfo;
import org.apache.cxf.service.model.BindingOperationInfo;
import org.apache.cxf.service.model.MessagePartInfo;
import org.apache.cxf.service.model.OperationInfo;
import org.apache.cxf.service.model.ServiceModelUtil;

public class Main {

    public static void main(String[] args) {
        URL wsdlURL = null;
        try {
            wsdlURL = new URL("http://path_to_wsdl?wsdl");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(wsdlURL, classLoader);
        Binding binding = client.getEndpoint().getBinding();
        BindingInfo bindingInfo = binding.getBindingInfo();
        Collection<BindingOperationInfo> operations = bindingInfo.getOperations();
        for(BindingOperationInfo boi:operations){
            OperationInfo oi = boi.getOperationInfo();
            BindingMessageInfo inputMessageInfo = boi.getInput();
            List<MessagePartInfo> parts = inputMessageInfo.getMessageParts();
            System.out.println("function name: "+oi.getName().getLocalPart());
            List<String> inputParams = ServiceModelUtil.getOperationInputPartNames(oi);
            System.out.println("input parameters: "+inputParams);
            for(MessagePartInfo partInfo:parts){
                Class<?> partClass = partInfo.getTypeClass();       //here we have input parameter object on each iteration
                Method[] methods = partClass.getMethods();
                for(Method method:methods){
                    System.out.println("method: "+method);
                    Class<?>[] paramTypes = method.getParameterTypes();
                    for(Class paramType:paramTypes){
                        System.out.println("param: "+paramType.getCanonicalName());                     
                    }
                    Class returnType = method.getReturnType();
                    System.out.println("returns: "+returnType.getCanonicalName());
                }
                System.out.println("partclass: "+partClass.getCanonicalName());
            }
        }
        System.out.println("binding: " + binding);
        }
}
4

1 回答 1

0

创建一个如下所示的绑定文件:

<jaxb:bindings
  xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0"
  xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionBindingPrefixes="xjc">

  <jaxb:globalBindings generateElementProperty="false">
    <xjc:simple />
  </jaxb:globalBindings>
</jaxb:bindings>

并通过采用绑定文件列表的 createClient 方法将其传递给 JaxWsDynamicClientFactory。

于 2013-09-09T18:20:24.427 回答