Web Service是如今最適合實現SOA的技術,而Axis是實現Web Service的一種技術框架(架構)。java
所須要的jar包:web
axis.jarapache
jaxrpc.jartomcat
saaj.jar架構
wsdl4j-1.5.1.jarapp
commons-discovery-0.2.jar框架
xbean.jaride
下載地址:http://download.csdn.net/detail/ch656409110/5747129ui
1 <!-- axis配置 --> 2 <listener> 3 <listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class> 4 </listener> 5 <servlet> 6 <display-name>Apache-Axis Servlet</display-name> 7 <servlet-name>AxisServlet</servlet-name> 8 <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class> 9 </servlet> 10 <servlet> 11 <display-name>Axis Admin Servlet</display-name> 12 <servlet-name>AdminServlet</servlet-name> 13 <servlet-class>org.apache.axis.transport.http.AdminServlet</servlet-class> 14 <load-on-startup>100</load-on-startup> 15 </servlet> 16 <servlet-mapping> 17 <servlet-name>AxisServlet</servlet-name> 18 <url-pattern>/servlet/AxisServlet</url-pattern> 19 </servlet-mapping> 20 <servlet-mapping> 21 <servlet-name>AxisServlet</servlet-name> 22 <url-pattern>/services/*</url-pattern> 23 </servlet-mapping>
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> 4 <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/> 5 6 <service name="houseWebService" provider="java:RPC"> 7 <parameter name="allowedMethods" value="*"/> 8 <parameter name="className" value="com.struts2.webservice.server.HouseWebServiceImpl"/> 9 </service> 10 11 <transport name="http"> 12 <requestFlow> 13 <handler type="URLMapper"/> 14 </requestFlow> 15 </transport> 16 </deployment>
一、定義接口HouseWebService.java url
public String getHouseList(String xmlStr);
二、實現該接口HouseWebServiceImpl.java
能夠隨便返回一些字符串便可。
在地址欄輸入:http://localhost:8080/house/services/houseWebService?wsdl 便可看到發佈的服務端的明細。
以下:
這就表示Axis發佈的webservice服務端成功了。
1 /** 2 * 調用webservice路口 3 * @param endpoint 地址 4 * @param methodName 調用的方法 5 * @param xmlStr 傳遞的xml字符串參數 6 * @return 7 */ 8 public String invoke(String endpoint,String methodName,String xmlStr){ 9 Service service = new Service(); 10 Call call = null; 11 try { 12 call = (Call) service.createCall(); 13 } catch (ServiceException e) { 14 e.printStackTrace(); 15 } 16 QName qn = new QName(methodName); 17 call.setOperationName(qn); 18 call.setTargetEndpointAddress(endpoint); 19 call.setUseSOAPAction(true); 20 String result = ""; 21 try { 22 // 給方法傳遞參數,而且調用方法 23 result = (String) call.invoke(new Object[] { xmlStr }); 24 } catch (RemoteException e) { 25 e.printStackTrace(); 26 } 27 System.out.println("result is " + result); 28 return result; 29 } 30 31 public List<House> getHouseList() { 32 String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" 33 + " <house>" 34 + " <condition>" 35 + " <title>便宜</title>" 36 + " <price></price>" 37 + " <streetname></streetname>" 38 + " <typename></typename>" 39 + " <floorage>100</floorage>" 40 + " <pageno></pageno>" 41 + " <pagesize></pagesize>" 42 + " </condition>" 43 + " </house>"; 44 String result = invoke("http://localhost:8080/house/services/houseWebService","getHouseList",xmlStr); 45 }
在這裏面傳遞的xml規範由 服務端本身規範好了,而後去解析、獲取參數,執行相應的操做,返回想要的結果給調用的客戶端。。
本文轉載於:http://blog.csdn.net/chenghui0317/article/details/9318317