CXF上的动态方法调用和JAXBElement类型

时间:2013-09-06 06:33:09

标签: apache jaxb wsdl cxf jaxbelement

我在下面编写了一个小应用程序,列出了使用Apache CXF库的所有方法和soap服务。此应用程序列出了服务的所有方法,但是当您运行此应用程序时在输出中可以看到,服务方法的输入参数和返回类型是复杂类型的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);
        }
}

1 个答案:

答案 0 :(得分: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。