1. 程式人生 > >axis1 創建service服務端 , axis1 客戶端

axis1 創建service服務端 , axis1 客戶端

ide add app pin endpoint remote down println out

axis1 服務端配置

1、首先建立一個項目 axisTest 不需多說

2、在lib下放入需要的jar包 點擊下載 :axis所需的jar包下載

3、然後需要在web.xml裏面加入:

<servlet>
    <servlet-name>AxisServlet</servlet-name>
    <display-name>Apache-Axis Servlet</display-name>
    <servlet-class>
        org.apache.axis.transport.http.AxisServlet
    </servlet-class>
 </servlet>
 <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/servlet/AxisServlet</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>*.jws</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
 </servlet-mapping>

4、創建服務端的代碼:

public class myService {
       public String getusername(String name){/**用spring的時候需要添加你要註入的bean
           baseTransaction = (BaseTransaction) getApplicationContext().getBean("baseTransaction")**/
           return "Hello "+name+",this is an Axis Web Service"; 
           }
   }  

5、在WEB-INF下創建service-config.wsdd 文件,內容如下:

<deployment xmlns="http://xml.apache.org/axis/wsdd/"  xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"/>   
   <service name="myService" provider="java:RPC">
       <!--這裏的name是你的service名字 訪問的時候要用得著的-->
       <parameter name="className" value="com.service.myService"/>
       <!--這裏的value是你所提供的的供外部訪問的方法所在的類-->
        <parameter name="allowedMethods" value="getusername"/>
        <!--供外部訪問的方法-->
    </service>
<transport name="http">
 <requestFlow>
    <handler type="URLMapper"/>
 </requestFlow>
</transport>
</deployment>  

6、啟動tomcat 訪問地址:http://localhost:8080/axisTest/servlet/AxisServlet

axis1 客戶端調用

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 myServiceTestorByWSDD {
    public static void main(String[] args) throws ServiceException,MalformedURLException, RemoteException {
    /**假如訪問的時候有spring註入的情況 則需要 添加
    baseTransaction = (BaseTransaction) getApplicationContext().getBean("baseTransaction"); 來獲取spring的註入信息。實在service層添加的。**/
    String endpoint = "http://localhost:8080/axisTest/services/myService";
    Service service = new Service(); // 創建一個Service實例,註意是必須的!
    Call call = (Call) service.createCall(); // 創建Call實例,也是必須的!
    call.setTargetEndpointAddress(new java.net.URL(endpoint));// 為Call設置服務的位置
    call.setOperationName("getusername"); // 註意方法名與JavaBeanWS.java中一樣!!
    String res = (String) call.invoke(new Object[] { "鄒萍" }); // 返回String,傳入參數
    System.out.println(res);    
    }
}

  

axis1 創建service服務端 , axis1 客戶端