Apache CXF上的动态方法调用和JAXBElement类型
我在下面编写了一个小应用程序,列出了使用ApacheCXF库的soap服务的所有方法和步骤。此应用程序列出了服务的所有方法,但正如运行此应用程序时在输出上看到的,对于复杂类型,服务方法的输入参数和返回类型是JAXBElement。我希望cxf不生成JAXBElement,而是希望在运行时生成原始类中的复杂类型。如上所述,可以通过将cxf库的wsdl2java实用程序的generateElementProperty属性的值设置为false来实现,但我无法找到与cxf库的动态方法调用相同的参数。我想获得原始类型中的输入参数和返回类型Apache CXF上的动态方法调用和JAXBElement类型,apache,jaxb,wsdl,cxf,jaxbelement,Apache,Jaxb,Wsdl,Cxf,Jaxbelement,我在下面编写了一个小应用程序,列出了使用ApacheCXF库的soap服务的所有方法和步骤。此应用程序列出了服务的所有方法,但正如运行此应用程序时在输出上看到的,对于复杂类型,服务方法的输入参数和返回类型是JAXBElement。我希望cxf不生成JAXBElement,而是希望在运行时生成原始类中的复杂类型。如上所述,可以通过将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);
}
}
import java.lang.reflect.Method;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.util.Collection;
导入java.util.List;
导入org.apache.cxf.binding.binding;
导入org.apache.cxf.endpoint.Client;
导入org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
导入org.apache.cxf.service.model.BindingInfo;
导入org.apache.cxf.service.model.BindingMessageInfo;
导入org.apache.cxf.service.model.BindingOperationInfo;
导入org.apache.cxf.service.model.MessagePartInfo;
导入org.apache.cxf.service.model.OperationInfo;
导入org.apache.cxf.service.model.ServiceModelUtil;
公共班机{
公共静态void main(字符串[]args){
URL wsdlURL=null;
试一试{
wsdlURL=新URL(“http://path_to_wsdl?wsdl");
}捕获(格式错误){
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();
集合操作=bindingInfo.getOperations();
用于(BindingOperationInfo-boi:operations){
OperationInfo oi=boi.getOperationInfo();
BindingMessageInfo-inputMessageInfo=boi.getInput();
List parts=inputMessageInfo.getMessageParts();
System.out.println(“函数名:+oi.getName().getLocalPart());
List inputParams=ServiceModelUtil.getOperationInputPartNames(oi);
System.out.println(“输入参数:“+inputParams”);
用于(MessagePartInfo partInfo:parts){
Class partClass=partInfo.getTypeClass();//这里我们在每次迭代中都有输入参数对象
方法[]方法=partClass.getMethods();
用于(方法:方法){
System.out.println(“方法:”+方法);
类[]paramTypes=method.getParameterTypes();
for(类paramType:paramTypes){
System.out.println(“param:+paramType.getCanonicalName());
}
类returnType=method.getReturnType();
System.out.println(“返回:”+returnType.getCanonicalName());
}
System.out.println(“partclass:+partclass.getCanonicalName());
}
}
System.out.println(“绑定:+binding”);
}
}
创建一个如下所示的绑定文件:
<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方法将其传递到JAXWSDynamicClient工厂。非常感谢。这个解决方案解决了这个问题。