标签:webservice实例
1.jws方式实例
编写一个服务类
public class HelloWorld{ public String sayHello(String name){ return "axis" +name; } }
将这个类的源文件HelloWorld.java 重命名为HelloWorld.jws,并将它复制到Tomcat/webapps/axis目录下,启动tomcat
编写客户端程序,调用上面的服务
package com.liyang.axis.client; import java.rmi.RemoteException; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.ServiceException; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; public class HelloWorld { /** * @param args * @throws ServiceException * @throws RemoteException */ public static void main(String[] args) throws ServiceException, RemoteException { String endpoint="http://localhost:8080/axis/HelloWorld.jws"; String name="liuyang"; org.apache.axis.client.Service service=new Service(); Call call=(Call) service.createCall(); call.setTargetEndpointAddress(endpoint); call.setOperationName("sayHello"); call.addParameter("param1", XMLType.XSD_STRING,ParameterMode.IN); call.setReturnType(XMLType.XSD_STRING); String set=(String) call.invoke(new Object[]{name}); System.out.println(set); } }运行,既可以得到结果跑
2.wsdd方式
编写一个服务类
编译这个类。并将class文件复制到tomcat/webapps/axis/web-inf/classes目录下
3.编写文件deploy.wsdd
public class HelloWorld{ public String <pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?> <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <globalConfiguration> <parameter name="adminPassword" value="admin"/> <parameter name="attachments.Directory" value="./attachments"/> <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/> <parameter name="sendXsiTypes" value="true"/> <parameter name="sendMultiRefs" value="true"/> <parameter name="sendXMLDeclaration" value="true"/> <parameter name="axis.sendMinimizedElements" value="true"/> <requestFlow> <handler type="java:org.apache.axis.handlers.JWSHandler"> <parameter name="scope" value="session"/> </handler> <handler type="java:org.apache.axis.handlers.JWSHandler"> <parameter name="scope" value="request"/> <parameter name="extension" value=".jwr"/> </handler> </requestFlow> </globalConfiguration> <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/> <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/> <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/> <service name="AdminService" provider="java:MSG"> <parameter name="allowedMethods" value="AdminService"/> <parameter name="enableRemoteAdmin" value="true"/> <parameter name="className" value="org.apache.axis.utils.Admin"/> <namespace>http://xml.apache.org/axis/wsdd/</namespace> </service> <service name="MyService" provider="java:RPC"> <parameter name="allowedMethods" value="*"/> <parameter name="className" value="com.myaxis.MyService"/> </service> <transport name="http"> <requestFlow> <handler type="URLMapper"/> <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/> </requestFlow> </transport> <transport name="local"> <responseFlow> <handler type="LocalResponder"/> </responseFlow> </transport> </deployment>
注意修改上面service的内容
打开命令行,将目录切换到deploy.wsdd所在的目录,
运行:java org.apache.axis.client.AdminClient deploy.wsdd
得到以下结果:
processing file deploy.wsdd
<admin>done processing</admin>
完成部署
编写客户端调用
package com.liyang.axis.client;
import java.rmi.RemoteException;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
public class HelloWorld {
/**
* @param args
* @throws ServiceException
* @throws RemoteException
*/
public static void main(String[] args) throws ServiceException, RemoteException {
String endpoint="http://localhost:8081/axis/services/MyService";
String name="liuyang";
org.apache.axis.client.Service service=new Service();
Call call=(Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.setOperationName("sayHello");
call.addParameter("param1", XMLType.XSD_STRING,ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);
String set=(String) call.invoke(new Object[]{name});
System.out.println(set);
}
}
标签:webservice实例
原文地址:http://blog.csdn.net/u013998070/article/details/44873181