java实现webservice实例
2.创建类以及需要对外提供的方法
package com.cn.test;
public class ServiceMehtod {
public String getInfo(String str1,String str2,String str3)
{
return "ServiceMehtod:getInfo() =====>str1="+str1+";str2="+str2+";str3="+str3;
}
}
2.创建server-config.wsdd文件(放在WebRoot/WEB-INF目录下,和wen.xml属于同一级目录)
<?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="sendMultiRefs" value="true" ></parameter>
<parameter name="disablePrettyXML" value="true"></parameter>
<parameter name="dotNetSoapEncFix" value="true"></parameter>
<parameter name="enableNamespacePrefixOptimization" value="false"></parameter>
<parameter name="sendXMLDeclaration" value="true"></parameter>
<parameter name="sendXsiTypes" value="true"></parameter>
<paramenter name="attachments.implementataion" value="org.apache.axis.attachments.AttachmentsImpl"></paramenter>
</globalConfiguration>
<handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"></handler>
<service name="Login.jws" provider="java:RPC">
<parameter name="className" value="com.cn.test.SayHello"/>
<parameter name="scope" value="request"></parameter>
<parameter name="allowedMethods" value="*"/>
<parameter class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"></parameter>
</service>
<service name="ServiceMethod.jws" provider="java:RPC">
<parameter name="className" value="com.cn.test.ServiceMehtod"></parameter>
<parameter name="scope" value="request"></parameter>
<parameter name="allowedMethods" value="*"></parameter>
<parameter name="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"></parameter>
</service>
<transport name="http">
<requestFlow>
<handler type="URLMapper"></handler>
</requestFlow>
</transport>
</deployment>
3.启动tomact,在浏览器地址栏里输入:http://localhost:8080/HelloWebService/services,就可以看见所提供的方法
4.编写Client代码
package com.cn.test.client;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class TestClient {
public static void main(String[] args) throws Exception{
getServiceMehtod();
}
public static void getServiceMehtod() throws Exception
{
//指出service所在的URL
String url="http://10.10.31.53:8080/HelloWebService/services/ServiceMethod.jws";
//创建一个服务调用
Service service=new Service();
//通过service创建call对象
Call call=(Call)service.createCall();
//设置service所在的URL
call.setTargetEndpointAddress(new java.net.URL(url));
//方法名与webservice里的方法名一致
call.setOperationName("getInfo");
String result=(String)call.invoke(new Object[]{"测试1","测试2","测试3"});
System.out.println(result);
}