我正在尝试创建动态WS客户端,并且我在使用ComplexType参数进行WS操作时遇到了一些问题。这是一个例子:
WebService的:
@WebMethod
public int testPerson(Person a) {
return a.getAge();
}
class Person {
private int age;
public Person() {
}
public Person(int i) {
this.age = i;
};
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
以下是我如何调用WS:
Client c = JaxWsDynamicClientFactory.newInstance().createClient("wsdlPath");
c.invoke("testPerson",...);
好的,我的问题是我应该传递什么参数来调用这个WebService(因为我说客户端必须是动态的,所以我不能将类Person导入客户端)?是否有可能我只传递原始类型的结构(在这种情况下是一个带有年龄参数的元素结构)?谢谢你的任何建议。
答案 0 :(得分:1)
如果您不打算使用复杂类型
,则无法使用JaxWsDynamicClientFactory
此外,从技术上讲,您不必将Person
类型导入客户端。您真正需要做的就是了解类型,并使用反射在运行时生成类的实例。
您在此处使用的createClient
版本仅适用于接受简单类型的Web服务操作。为了能够将复杂类型传递给动态Web服务客户端,
JaxWsDynamicClientFactory
需要使用以下内容动态生成必要的支持类:
ClassLoader loader = this.getClass().getClassLoader();
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("wsdlPath", classLoader);
这会创建Client
对象以及必要的pojos。
然后您就可以通过以下方式致电该服务:
//Dynamically load an instance of the Person class. You're not importing and you can simply configure the class name as an application property
Object person = Thread.currentThread().getContextClassLoader().loadClass("foo.bar.Person").newInstance();
Method theMethod = person.getClass().getMethod("setAge", Integer.class);
theMethod.invoke(person, 55); //set a property
client.invoke("testPerson", person); //invoke the operation.
除非采用上述方法,否则唯一的另一种方法是使用Dispatch API
手动构建SOAP有效负载。这是一种艰苦的方法(确保它是你想要的)。
最终,这两种方法都要求您对Web服务调用期间要处理的类型有一些预知