java使用axis1.4调用WebService简单示例

前提条件 :下载axis安装包(url:  http://ws.Apache.org/axis/ 
1.首先,创建web工程(项目名称:ServiceTest),将axis中lib中的jar包拷贝到/WEB-INF/lib目录下,然后将这些包添加到项目中。
2.写配置文件:server-config.wsdd 放在/WEB-INF 目录下
<?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>  

3.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>ServiceTest</display-name>
    
          <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>  
    
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

4.
//服务器端提供的方法:
package server;

public class SayHello {
    
    public String getName(String name){
        return "Hello ,"+name;
    }
}

//客户端访问服务端接口获得数据的方法
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/ServiceTest/services/Login.jws";
        
        //创建一个服务(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);
    }
}


5.启动tomcat,先访问http://localhost:8080/ServiceTest/services,查看是否有方法显示,如果有显示,则代表服务端提供的接口可以成功访问了。

我的效果是这样:

And now... Some Services

·                  Login.jws (wsdl)

o                          getName

6.在本地run客户端TestClient,就应该输出结果了。

你可能感兴趣的:(java使用axis1.4调用WebService简单示例)