webservice例子了
Axis支持三种web service的部署和开发,分别为:
1、Dynamic Invocation Interface ( DII)
2、Stubs方式
3、Dynamic Proxy方式
一、编写DII(Dynamic Invocation Interface)方式web服务
1.编写服务端程序DIIService
public class DIIService {
public String getName(String name) {
System.out.println("=====DIIService=====");
return "The " + name + " return from the DIIService" ;
}
}
2、将源码拷贝到Axis_HOME下,重命名为 DIIService.jws
3、访问连接http://localhost:8080/Axis/DIIService.jws?wsdl,页面显示Axis自动生成的wsdl
4、编写访问服务的客户端 DIIClient.java
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
public class DIIClient {
public static void main(String[] args) throws ServiceException,
MalformedURLException, RemoteException {
String endpoint = "http://localhost:8090/axis/DIIService.jws";
Service service = new Service();
Call call = null;
call = (Call) service.createCall();
call.setOperationName(new QName(endpoint, "getName"));
call.setTargetEndpointAddress(new java.net.URL(endpoint));
// call.addParameter("param1", XMLType.XSD_STRING, ParameterMode.IN);
// call.setReturnType(XMLType.XSD_STRING);
String ret = (String) call.invoke(new Object[] { "zhangsan" });
System.out.println("return value is ==> " + ret);
}
}
二、编写Dynamic Proxy方式访问服务
1、编写部署服务端程序DPService
public class DPService {
public String getName(String name) {
System.out.println("=====DPService=====");
return "The " + name + " return from the DPService";
}
}
2、编写代理接口
import java.rmi.Remote;
public interface DPServiceInterface extends Remote {
public String getName(String name) throws java.rmi.RemoteException;
}
3、编写并执行客户端程序DPClient.java
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import java.net.URL;
import javax.xml.namespace.QName;
public class DPClient {
public static void main(String[] args) {
try {
String wsdlUrl = "http://localhost:8090/axis/DPService.jws?wsdl";
String nameSpaceUri = "http://localhost:8090/axis/DPService.jws";
String serviceName = "DPServiceService";
String portName = "DPService";
ServiceFactory serviceFactory = ServiceFactory.newInstance();
Service afService = serviceFactory.createService(new URL(wsdlUrl),
new QName(nameSpaceUri, serviceName));
DPServiceInterface proxy = (DPServiceInterface) afService
.getPort(new QName(nameSpaceUri, portName),
DPServiceInterface.class);
System.out.println("return value is " + proxy.getName("john"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
四、编写stubs方式web服务
1、
public class StubsService {
//转到AXIS目录下的WEB-INF子目录
//Java -Djava.ext.dirs=lib org.apache.axis.wsdl.WSDL2Java http://localhost:8080/axis/StubsService.jws?wsdl
public String getName(String name) {
System.out.println("=====StubsService=====");
return "The " + name + " return from the StubsService";
}
}
2、Axis可选的包:activation.jar; mail.jar; xmlsec-1.4.Beta1.jar拷贝到WEB-INF目录下.转到AXIS目录下的WEB-INF子目录执行Java -Djava.ext.dirs=lib org.apache.axis.wsdl.WSDL2Java http://localhost:8080/axis/StubsService.jws?wsdl
执行后会在WEB-INF\localhost\axis\StubsService_jws生成
StubsService.java,StubsServiceService.java,StubsServiceServiceLocator.java,StubsServiceSoapBindingStub.java
3、编写并执行客户端程序
package localhost.axis.StubsService_jws;
public class Main {
public static void main(String[] args) throws Exception
{
StubsServiceService service = new StubsServiceServiceLocator();
StubsService stubs = service.getStubsService();
System.out.println("Response:"+stubs.getName("BeatBLOG"));
}
}
Axis支持三种web service的部署和开发,分别为:
1、Dynamic Invocation Interface ( DII)
2、Stubs方式
3、Dynamic Proxy方式
一、编写DII(Dynamic Invocation Interface)方式web服务
1.编写服务端程序DIIService
public class DIIService {
public String getName(String name) {
System.out.println("=====DIIService=====");
return "The " + name + " return from the DIIService" ;
}
}
2、将源码拷贝到Axis_HOME下,重命名为 DIIService.jws
3、访问连接http://localhost:8080/Axis/DIIService.jws?wsdl,页面显示Axis自动生成的wsdl
4、编写访问服务的客户端 DIIClient.java
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
public class DIIClient {
public static void main(String[] args) throws ServiceException,
MalformedURLException, RemoteException {
String endpoint = "http://localhost:8090/axis/DIIService.jws";
Service service = new Service();
Call call = null;
call = (Call) service.createCall();
call.setOperationName(new QName(endpoint, "getName"));
call.setTargetEndpointAddress(new java.net.URL(endpoint));
// call.addParameter("param1", XMLType.XSD_STRING, ParameterMode.IN);
// call.setReturnType(XMLType.XSD_STRING);
String ret = (String) call.invoke(new Object[] { "zhangsan" });
System.out.println("return value is ==> " + ret);
}
}
二、编写Dynamic Proxy方式访问服务
1、编写部署服务端程序DPService
public class DPService {
public String getName(String name) {
System.out.println("=====DPService=====");
return "The " + name + " return from the DPService";
}
}
2、编写代理接口
import java.rmi.Remote;
public interface DPServiceInterface extends Remote {
public String getName(String name) throws java.rmi.RemoteException;
}
3、编写并执行客户端程序DPClient.java
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import java.net.URL;
import javax.xml.namespace.QName;
public class DPClient {
public static void main(String[] args) {
try {
String wsdlUrl = "http://localhost:8090/axis/DPService.jws?wsdl";
String nameSpaceUri = "http://localhost:8090/axis/DPService.jws";
String serviceName = "DPServiceService";
String portName = "DPService";
ServiceFactory serviceFactory = ServiceFactory.newInstance();
Service afService = serviceFactory.createService(new URL(wsdlUrl),
new QName(nameSpaceUri, serviceName));
DPServiceInterface proxy = (DPServiceInterface) afService
.getPort(new QName(nameSpaceUri, portName),
DPServiceInterface.class);
System.out.println("return value is " + proxy.getName("john"));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
四、编写stubs方式web服务
1、
public class StubsService {
//转到AXIS目录下的WEB-INF子目录
//Java -Djava.ext.dirs=lib org.apache.axis.wsdl.WSDL2Java http://localhost:8080/axis/StubsService.jws?wsdl
public String getName(String name) {
System.out.println("=====StubsService=====");
return "The " + name + " return from the StubsService";
}
}
2、Axis可选的包:activation.jar; mail.jar; xmlsec-1.4.Beta1.jar拷贝到WEB-INF目录下.转到AXIS目录下的WEB-INF子目录执行Java -Djava.ext.dirs=lib org.apache.axis.wsdl.WSDL2Java http://localhost:8080/axis/StubsService.jws?wsdl
执行后会在WEB-INF\localhost\axis\StubsService_jws生成
StubsService.java,StubsServiceService.java,StubsServiceServiceLocator.java,StubsServiceSoapBindingStub.java
3、编写并执行客户端程序
package localhost.axis.StubsService_jws;
public class Main {
public static void main(String[] args) throws Exception
{
StubsServiceService service = new StubsServiceServiceLocator();
StubsService stubs = service.getStubsService();
System.out.println("Response:"+stubs.getName("BeatBLOG"));
}
}