【问题标题】:Dynamic Method Invocation and JAXBElement Type on CXFCXF 上的动态方法调用和 JAXBElement 类型
【发布时间】:2013-09-06 06:33:08
【问题描述】:

我编写了下面的小应用程序来列出使用 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);
        }
}

【问题讨论】:

    标签: apache jaxb wsdl cxf jaxbelement


    【解决方案1】:

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

    <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。

    【讨论】:

    • 非常感谢丹尼尔。这个解决方案解决了这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多