j2ee web service 客户端的三种调用方式:
(一)占位程序:该种调用方式需要服务端生成所谓的占位程序,也是性能最好的一种方式,据有的文档介绍说用占位程序实现的web服务不能实现各种编程语言比如C#之是互相调用就是会引起互操作性的问题.而且生成占位程序的web服务的确稍麻烦一点,更重要的是我不喜欢这种方式,也就不作备忘了:)。
(二)动态代理:该种方式需要一个指向WSDL文档的URL。具体实现:
ServiceFactoryImpl factory = new ServiceFactoryImpl();
// 工厂类,取得service对象,
URL wsdlURL = new File( "complexType-array/WEB-INF/wsdl/CountUser.wsdl").toURL();
//wsdl 文档的URL 它可以是一个远程的URL 但是本例引用本地硬盘上的一个wsdl文件,其好处是提高程序的性能。
URL mappingURL = new File("complexType-array/WEB-INF/CountUser.xml").toURL();
// 映射文件的URL 需要着重说明的就是这个映射文件了,标准的j2ee web service API实现中可不需要这玩意,但是在Jboss的实现中不要这个就会报错cannot obtain java mapping type...,在jboss下开发的web 服务客户端移植时需要特别注意,麻烦!
QName qname = new QName("http://array", "CountUserService");
Service service = factory.createService(wsdlURL, qname, mappingURL);
// 通过工厂方法得到一个Service对象,但createService(wsdlURL, qname, mappingURL)方法是jboss的具体实现增加一的一个方法,标准API可没有这玩意,对于数组类型的传递只能用这个方法了,奇怪的是客户端居然需要部署在服务器端的映射文件,没劲!
CountUser port = (CountUser) service.getPort(CountUser.class);
// 取得服务器端的接口。
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
( 三)动态调用。
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");// 表示服务名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});
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
服务器端的实现参见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);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
至此好象一个可以应用的J2EE web service服务就完成了,然而这才仅仅只是开始~~~~~~~~~
【编辑推荐】