WebServices Axis使用

WebServices Axis使用记录。

1)在服务端定义接口:
package demo;

import java.rmi.Remote;
public interface MyInterface extends Remote {

    void test(String id, String name);
}

2)实现已经定义的接口:
package demo;

import demo.MyInterface;
public class Service implements MyInterface {
    @Override
    public void test(String id, String name) {
        System.out.println("id: " + id + "; name: " + name);
    }
}


3)配置文件demo.wsdd:
<?xml version="1.0" encoding="GB2312"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
    <service name="MyInterface" provider="java:RPC" style="rpc" use="encoded">
        <parameter name="className" value="demo.Service" />
        <parameter name="allowedMethods" value="*" />
    </service>
</deployment>


4)客户端代码:
package demo.client;

public class ClientDemo {

    public static void demo(String id, String name) throws Exception {

        String url = "http://127.0.0.1:7001/Demo/services/MyInterface"; //服务端部署的url。

        org.apache.axis.client.Service ws = new org.apache.axis.client.Service();
        org.apache.axis.client.Call call = (org.apache.axis.client.Call) ws.createCall();
        call.setTargetEndpointAddress(url);
        call.setOperationName("test");
        call.setTimeout(30000);

        java.lang.Object ret = call.invoke(new java.lang.Object[] { id, name });
        if (ret instanceof java.rmi.RemoteException) {
            throw (java.rmi.RemoteException) ret;
        }
    }
}


你可能感兴趣的:(java,webservice)