首先建立这个 webservice 工程 file>new web service project
JAVA类
package client;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class TestClient {
public static void main(String[] args) throws ServiceException, MalformedURLException, RemoteException {
String endpoint = "http://localhost:8080/AxisWebService/services/Login";
//创建一个服务(service)调用(call)
Service service = new Service();
Call call = (Call) service.createCall();
//设置service所在的url
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperation("getName");
String ret = (String)call.invoke(new Object[]{"继中"});
System.out.println(ret);
}
}
然后在 web.XML 更改以下
<!-- WebService核心处理类 -->
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- ********************************************** --></web-app>
在 web.xml 同级创建service-config.wsdd
<?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 name="disablePrettyXML" value="true"/>
<parameter name="dotNetSoapEncFix" value="true"/>
<parameter name="enableNamespacePrefixOptimization" value="false"/>
<parameter name="sendXMLDeclaration" value="true"/>
<parameter name="sendXsiTypes" value="true"/>
<parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/>
</globalConfiguration>
<handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"/>
<service name="hello" provider="java:RPC">
<parameter name="className" value="server.SayHello"/>
<parameter name="scope" value="request"/>
<parameter name="allowedMethods" value="*"/>
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
</service>
<transport name="http">
<requestFlow>
<handler type="URLMapper"/>
</requestFlow>
</transport>
</deployment>
启动tomcat测试 浏览器查看是否正常http://localhost:8080/AxisWebService/services
查看是否有方法显示,如果有显示,则代表服务端提供的接口可以成功访问了。
然后创建 webclient
t同上做法
package client;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class TestClient {
public static void main(String[] args) throws ServiceException, MalformedURLException, RemoteException {
String endpoint = "http://localhost:8080/AxisWebService/services/hello";
//创建一个服务(service)调用(call)
Service service = new Service();
Call call = (Call) service.createCall();
//设置service所在的url
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperation("getName");
String ret = (String)call.invoke(new Object[]{"会跳的程序员"});
System.out.println(ret);
}
}
执行xx.bat文件 发布下
最后在client run以下输出 Hello,会跳的程序员
源代码下载地址:https://download.youkuaiyun.com/download/u012374381/10284551