动态Web服务客户端和Java反射

时间:2014-06-27 09:34:03

标签: java web-services reflection wsdl

Webservice包含:

ResultObj resultObj = getDocCountAction(RequestObj requestObj);

其中:

ResultObj and RequestObj contain "Long count" attribute.

所以,webservis方法依赖输入并返回输出计数(我知道 - 这是废话:)

我想要“client.invoke(”getDocCountAction“,requestObj);”将值返回给responseObj。默认情况下,它返回Object []。

// webservice client from remote wsdl
String wsdlURL = "http://localhost:8080/test/test.wsdl"
ClassLoader loader = Thread.currentThread().getContextClassLoader();
DynamicClientFactory factory = DynamicClientFactory.newInstance();
Client client = factory.createClient(wsdlURL, loader);

// accessing request object and setter method for count attribute and setting 666 value
Object requestObj = Thread.currentThread().getContextClassLoader().loadClass("pl.kago.stuff.RequestObj").newInstance();
Method setCount = requestObj.getClass().getMethod("setCount", Long.class);
setCount.invoke(requestObj, 666);

现在我有问题了。我知道我必须调用webservice方法并定义和访问responseObj。 如何使用responseObj“绑定”webmethod的结果?

// accessing response object and getter method for count attribute
Object responseObj = Thread.currentThread().getContextClassLoader() .loadClass("pl.kago.stuff.ResponseObj").newInstance();
Method getCount = responseObj.getClass().getMethod("getCount", Long.class);
client.invoke("getCount", responseObj);

以下访问webmethod

Object[] result = client.invoke("getCountAction", requestObj);

1 个答案:

答案 0 :(得分:0)

嘿:)

对于遇到此类问题的其他人:

// webservice client from remote wsdl
String wsdlURL = "http://localhost:8080/test/test.wsdl"
ClassLoader loader = Thread.currentThread().getContextClassLoader();
DynamicClientFactory factory = DynamicClientFactory.newInstance();
Client client = factory.createClient(wsdlURL, loader);

// accessing request object and setter method for count attribute and setting 666 value
Object requestObj = Thread.currentThread().getContextClassLoader().loadClass("pl.kago.stuff.RequestObj").newInstance();
Method setCount = requestObj.getClass().getMethod("setCount", Long.class);
setCount.invoke(requestObj, 666);

// "binding" new result object to webmethod result
Object responseObj = Thread.currentThread().getContextClassLoader() .loadClass("pl.kago.stuff.ResponseObj").newInstance();
responseObj = client.invoke("getCountAction", requestObj);

// getting count value
Method getCount = responseObj .getClass().getMethod("getCount");
Object count = getCount.invoke(responseObj);
相关问题