今天下午忽然想研究下WEBSERVICE,從網上找了好多實例,但不少寫的讓人實在難以恭維,花了三個小時的圈圈轉後總算搞通了,其實也挺簡單的,這裏特別感謝http://www.liuzm.com/article/java/992.htm這篇文章,總算讓我弄明白了,如今貼出個人實例步驟,感謝那個博主的同時,與你們共同分享。java
一:web
首先建立個WEB工程,而後: http://ws.Apache.org/axis/網站下載Axis安裝包.固然還依賴其餘包的,我這裏在附件裏上傳了全部應用到得包,方便你們。apache
二:數組
而後就寫wsdd 文件(注:其實這裏真的不用,能夠跳過第二步)爲了生成這個文件:server-config.wsddtomcat
deploy.wsdd:app
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="" target="_blank">http://xml.apache.org/axis/wsdd/providers/java"> <service name="HelloWorld" provider="java:RPC"> <parameter name="className" value="HelloWorld"/> <parameter name="allowedMethods" value="sayHello"/> </service> </deployment>webapp
在DOS下轉換目錄到%TOMCAT_HOME%\webapps\axis\WEB-INF,命令: java -cp %AXISCLASSPATH% org.apache.axis.client.AdminClient deploy.wsddide
三:網站
能夠本身寫第二步中的文件,代碼以下:url
- <?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="Login.jws" 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>
放到 WEB-INF 目錄下 和web.xml一塊兒,其中,WEB.XML能夠直接從你下載的axis項目中拿來到本身工程就好,或者本身添加:
- <servlet>
- <servlet-name>AxisServlet</servlet-name>
- <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>AxisServlet</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
四:
服務端提供的方法:
- package server;
-
- public class SayHello {
- public String getName(String name) {
- return "hello====>" + name;
- }
- }
五:
客戶端訪問服務端接口得到數據的方法:
- package client;
-
- import org.apache.axis.client.Call;
- import org.apache.axis.client.Service;
-
- public class TestClient {
-
- public static void main(String[] args) throws Exception {
-
-
-
- String endpoint = "http://127.0.0.1:8082/webservice/services/Login.jws";
-
-
-
- Service service = new Service();
-
- Call call = (Call) service.createCall();
-
-
-
- call.setTargetEndpointAddress(new java.net.URL(endpoint));
-
-
-
- call.setOperationName("getName");
-
-
-
- String ret = (String) call.invoke(new Object[] { "繼中" });
-
- System.out.println(ret);
-
- }
-
- }
六:
啓動tomcat,先訪問http://127.0.0.1:8082/webservice/services,看是否有方法顯示,有顯示,則表明服務端提供的接口能夠成功訪問了。
個人效果是這樣:
And now... Some Services
七:
而後在本地跑客戶端TestClient,就應該有結果啦,你們試試吧。