webService

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"));
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值