(二)動態(tài)代理:該種方式需要一個(gè)指向WSDL文檔的URL。具體實(shí)現(xiàn):
ServiceFactoryImpl factory = new ServiceFactoryImpl();
// 工廠類,取得service對象,
URL wsdlURL = new File( "complexType-array/WEB-INF/wsdl/CountUser.wsdl"). toURL(); //wsdl 文檔的URL 它可以是一個(gè)遠(yuǎn)程的URL 但是本例引用本地硬盤上的一個(gè)wsdl文件
其好處是提高程序的性能。
URL mappingURL = new File("complexType-array/WEB-INF/CountUser.xml"). toURL(); // 映射文件的URL 需要著重說明的就是這個(gè)映射文件了,標(biāo)準(zhǔn)的j2ee web service API實(shí)現(xiàn)中可不需要這玩意,但是在Jboss的實(shí)現(xiàn)中不要這個(gè)就會報(bào)錯(cuò)cannot obtain java mapping type...,在jboss下開發(fā)的web 服務(wù)客戶端移植時(shí)需要特別注意,麻煩!
QName qname = new QName("http://array", "CountUserService");
Service service = factory.createService(wsdlURL, qname, mappingURL);// 通過工廠方法得到一個(gè)Service對象,但createService(wsdlURL, qname, mappingURL)方法是jboss的具體實(shí)現(xiàn)增加一的一個(gè)方法,標(biāo)準(zhǔn)API可沒有這玩意,對于數(shù)組類型的傳遞只能用這個(gè)方法了,奇怪的是客戶端居然需要部署在服務(wù)器端的映射文件,沒勁!
CountUser port = (CountUser) service.getPort(CountUser.class); // 取得服務(wù)器端的接口。
( 三)動態(tài)調(diào)用。
URL wsdlURL = new File(
"complexType-array/WEB-INF/wsdl/CountUser.wsdl").
toURL();
URL mappingURL = new File(
"complexType-array/WEB-INF/CountUser.xml").
toURL();
QName qname = new QName("http://array", "CountUserService");// 表示服務(wù)名QName對象。
Service service = factory.createService(wsdlURL, qname, mappingURL);
Call call = service.createCall();
// 沒什么好說的,依然要用到wsdl文檔文件,映射文件。
call.setOperationName(new QName(TARGET_NAMESPACE, "countUser"));// 指定方法名
call.setPortTypeName(new QName("CountUser"));// 指定端口名
("value",Constants.TYPE_LITERAL_ANYSIMPLETYPE,ParameterMode.IN);
call.setReturnType(Constants.TYPE_LITERAL_INT);
Object retObj = call.invoke(new Object[] {user});
服務(wù)器端的實(shí)現(xiàn)參見j2ee web service(一)完整的客戶端代碼如下:
package array;
import java.net.URL;
import javax.xml.rpc.*;
import javax.xml.namespace.QName;
import java.util.*;
import java.io.File;
import org.jboss.ws.jaxrpc.ServiceFactoryImpl;
import org.jboss.ws.Constants;
public class ArrayExample {
public ArrayExample() {
}
private static final String TARGET_NAMESPACE =
"http://array";
private CountUser getPort() throws Exception {
ServiceFactoryImpl factory = new ServiceFactoryImpl();
URL wsdlURL = new File(
"complexType-array/WEB-INF/wsdl/CountUser.wsdl").
toURL();
URL mappingURL = new File(
"complexType-array/WEB-INF/CountUser.xml").
toURL();
QName qname = new QName("http://array", "CountUserService");
Service service = factory.createService(wsdlURL, qname, mappingURL);
CountUser port = (CountUser) service.getPort(CountUser.class);
return port;
}
public void testComplexUserArray(User[] user) throws
Exception {
CountUser port = getPort();
try {
int returnValue = port.countUser(user);
System.out.print(returnValue);
} catch (Exception e) {
throw e;
}
}
public void DIIClient(User[] user) throws Exception {
ServiceFactoryImpl factory = new ServiceFactoryImpl();
URL wsdlURL = new File(
"complexType-array/WEB-INF/wsdl/CountUser.wsdl").
toURL();
URL mappingURL = new File(
"complexType-array/WEB-INF/CountUser.xml").
toURL();
QName qname = new QName("http://array", "CountUserService");
Service service = factory.createService(wsdlURL, qname, mappingURL);
Call call = service.createCall();
call.setOperationName(new QName(TARGET_NAMESPACE, "countUser"));
call.setPortTypeName(new QName("CountUser"));
call.setReturnType(Constants.TYPE_LITERAL_INT);
Object retObj = call.invoke(new Object[] {user});
System.out.println(retObj.toString());
}
public static void main(String[] args) throws Exception {
ArrayExample arrayexample = new ArrayExample();
User[] user = new User[2];
user[0] = new User(" 張三", "027-88888888", new Date());
user[1] = new User("lisi", null, new Date());
//arrayexample.testComplexUserArray(user);
arrayexample.DIIClient(user);
}
}
如果你想賜教或者討論,歡迎加入QQ群: