其中的各个任务都有说明.
注意,如果你使用JAX-RPC1.1.2的话,wsdeploy生成web.xml文件有些小错误,需要手工更改,这个问题在1.1.3中已经修正.
好了,在Eclipse中运行各个任务,把最后得到的skysoft.war,作为一个新的应用发布,也可发布到你现有的应用,需要解压这个文件,然后拷贝需要的文件以及库文件到你的应用中即可.
(四) 调用服务 调用服务的方式有三种:静态调用,动态调用,动态调用接口.
1 静态调用,使用了wscompile生成的静态桩文件.
package hello;
import javax.xml.rpc.Stub;
import staticstub.*;
public class HelloClient {
private String endpointAddress;
public static void main(String[] args) {
args=new String[]{"http://localhost:8080/skysoft/hello?WSDL"};
System.out.println("Endpoint address = " + args[0]);
try {
Stub stub = createProxy();
stub._setProperty(javax.xml.rpc.Stub.ENDPOINT_ADDRESS_PROPERTY,
args[0]);
HelloIF hello = (HelloIF) stub;
System.out.println(hello.sayHello("Duke!"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static Stub createProxy() {
// Note: MyHelloService_Impl is implementation-specific.
return (Stub) (new MyHelloService_Impl().getHelloIFPort());
}
}
2 动态调用,主要是使用ServiceFactory来执行调用.
package hello;
import java.net.URL;
import javax.xml.rpc.Service;
import javax.xml.rpc.JAXRPCException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceFactory;
//import dynamicproxy.HelloIF;
import staticstub.HelloIF;
public class DynClient {
public static void main(String[] args) {
try {
args = new String[] { "http://localhost:8080/skysoft/hello" };
String UrlString = args[0] + "?WSDL";
String nameSpaceUri = "urn:Foo";
String serviceName = "MyHelloService";
String portName = "HelloIFPort";
System.out.println("UrlString = " + UrlString);
URL helloWsdlUrl = new URL(UrlString);
ServiceFactory serviceFactory = ServiceFactory.newInstance();
Service helloService = serviceFactory.createService(helloWsdlUrl,
new QName(nameSpaceUri, serviceName));
HelloIF myProxy = (HelloIF) helloService.getPort(new QName(
nameSpaceUri, portName), HelloIF.class);
System.out.println(myProxy.sayHello("Buzz"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
3 动态调用接口(DII),这种方式不需要任何附加代码,根据WSDL提供的调用方法的元描述,动态确定方法及其参数返回类型,有些类似于CORBA的接口池提供的服务.
package hello;
import javax.xml.rpc.Call;
import javax.xml.rpc.Service;
import javax.xml.rpc.JAXRPCException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.ParameterMode;
public class DiiClient {
private static String qnameService = "MyHelloService";
private static String qnamePort = "HelloIF";
private static String BODY_NAMESPACE_VALUE = "urn:Foo";
private static String ENCODING_STYLE_PROPERTY = "javax.xml.rpc.encodingstyle.namespace.uri";
private static String NS_XSD = "http://www.w3.org/2001/XMLSchema";
private static String URI_ENCODING = "http://schemas.xmlsoap.org/soap/encoding/";
public static void main(String[] args) {
args=new String[]{"http://localhost:8080/skysoft/hello?WSDL"};
System.out.println("Endpoint address dii = " + args[0]);
try {
ServiceFactory factory = ServiceFactory.newInstance();
Service service = factory.createService(new QName(qnameService));
QName port = new QName(qnamePort);
Call call = service.createCall(port);
call.setTargetEndpointAddress(args[0]);
call.setProperty(Call.SOAPACTION_USE_PROPERTY, new Boolean(true));
call.setProperty(Call.SOAPACTION_URI_PROPERTY, "");
call.setProperty(ENCODING_STYLE_PROPERTY, URI_ENCODING);
QName QNAME_TYPE_STRING = new QName(NS_XSD, "string");
call.setReturnType(QNAME_TYPE_STRING);
call.setOperationName(new QName(BODY_NAMESPACE_VALUE, "sayHello"));
call.addParameter("String_1", QNAME_TYPE_STRING, ParameterMode.IN);
String[] params = { "Murph!" };
String result = (String) call.invoke(params);
System.out.println(result);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
到此,这个Web Service入门描述就结束了,它提供了WS的Sun描述,以及在tomcat上发布服务的过程和一个构建脚本,借助jsp插件,使用Eclipse完成了一个简单的Web服务开发,发布,并用三种方式进行了测试.