1、在项目WEB-INF目录下有一个固定命名的配置文件server-config.wsdd,在该文件中作如下配置
<service name="demo" provider="java:RPC">
<parameter name="allowedMethods" value="*" />
<parameter name="className" value="hx.server.test.Demo" />
</service>
2、在web.xml中配置项目AxisServlet,如下:
<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/dataSync/*</url-pattern>
</servlet-mapping>
3、Demo类如下:
package hx.server.test;
public class Demo {
public String med1(String name){
return "Hello, " + name;
}
}
4、客户端调用,需要导入相应的axis.jar包:
package hx.client;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class TestCall {
public static void main(String[] args) {
try {
/ 项目名/AxisServlet(web.xml)/server-config.wsdd配置文件中 的service名称
String endpoint_1 = "http://192.168.0.123:8080/gagbsync/services/dataSync/demo?wsdl";
Service service = new Service();
Call call = (Call) service.createCall();
call.setOperationName(new QName(endpoint_1, "med1")); // "med1" 为方法名
call.setTargetEndpointAddress(new URL(endpoint_1));
String result = (String) call.invoke(new Object[] { "小明" });
System.out.println("result=" + result);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}
}
}