采用axis创建web service
1.安装axis,下载axis,将saaj.jar,jaxrpc.jar,axis.jar,commons-discovery-0.2.jar,
commons-logging-api.jar,log4j-1.2.8.jar,wsdl4j-1.5.1.jar,activation.jar,
mailapi.jar文件复制到tomcat的common\lib下,并加到classpath中
2.编写webservice服务
public class smsServer {
public int Send(Message msg)
{
...
}
public Message[] Recv()
{
...
}
public SendState[] CheckSate(String SubmitIDs)
{
...
}
}
3.编写wsdd发布
先编写wsdd文件,然后执行java org.apache.axis.client.AdminClient -lhttp://localhost:8080/SXDL_SMS/services/AdminService deploy.wsdd发布生成server-config.wsdd文件
4.生成wsdl
java org.apache.axis.wsdl.Java2WSDL -oWebsms.wsdl -lhttp://localhost:8080/SXDL_SMS/services/ -nWebsms zxsms.webservice.smsServer
5.生成客户端文件
java org.apache.axis.wsdl.WSDL2Java Websms.wsdl -p zxsms.webclient
6.客户端注册自定义结构,由于我采用的是自定义接口,因此客户端需要注册示例如下
public int send(zxsms.webclient.Message msg) throws java.rmi.RemoteException {
if (super.cachedEndpoint == null) {
throw new org.apache.axis.NoEndPointException();
}
org.apache.axis.client.Call _call = createCall();
javax.xml.namespace.QName qn=new javax.xml.namespace.QName("MessageBean", "MessageBean");
_call.registerTypeMapping(Message.class, qn,
new org.apache.axis.encoding.ser.BeanSerializerFactory(Message.class, qn),
new org.apache.axis.encoding.ser.BeanDeserializerFactory(Message.class, qn));
_call.setOperation(_operations[0]);
_call.setUseSOAPAction(true);
_call.setSOAPActionURI("");
_call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);
_call.setOperationName(new javax.xml.namespace.QName("Websms", "Send"));
setRequestHeaders(_call);
setAttachments(_call);
try { java.lang.Object _resp = _call.invoke(new java.lang.Object[] {msg});
if (_resp instanceof java.rmi.RemoteException) {
throw (java.rmi.RemoteException)_resp;
}
else {
extractAttachments(_call);
try {
return ((java.lang.Integer) _resp).intValue();
} catch (java.lang.Exception _exception) {
return ((java.lang.Integer) org.apache.axis.utils.JavaUtils.convert(_resp, int.class)).intValue();
}
}
} catch (org.apache.axis.AxisFault axisFaultException) {
throw axisFaultException;
}
}
其中下列代码是手工添加的
javax.xml.namespace.QName qn=new javax.xml.namespace.QName("MessageBean", "MessageBean");
_call.registerTypeMapping(Message.class, qn,
new org.apache.axis.encoding.ser.BeanSerializerFactory(Message.class, qn),
new org.apache.axis.encoding.ser.BeanDeserializerFactory(Message.class, qn));
javax.xml.namespace.QName qn=new javax.xml.namespace.QName("SendStateBean", "SendStateBean");
_call.registerTypeMapping(SendState.class, qn,
new org.apache.axis.encoding.ser.BeanSerializerFactory(SendState.class, qn),
new org.apache.axis.encoding.ser.BeanDeserializerFactory(SendState.class, qn));
7.客户端测试
public static void main(String[] args) {
try{
SmsServerService service = new SmsServerServiceLocator();
SmsServer_PortType smsclient = service.getsmsServer();
//身份认证
((org.apache.axis.client.Stub)smsclient).setUsername("001");
((org.apache.axis.client.Stub)smsclient).setPassword("001");
Message sms=new Message();
sms.setTo("13001430090");
sms.setContent("zyg webservice 短信 测试");
sms.setFormat(15);
long id=smsclient.send(sms);
System.out.println("submitid="+id);
} catch (Exception e) {
System.err.println("Execution failed. Exception: " + e);
}
}