Web Service实例

1、建立动态Web工程(Dynamic Web Project),工程名为webService。

package net.xqx.webservice;

public class CalculateService {
    public float plus(float x, float y) {
        return x + y;
    }

    public float minus(float x, float y) {
        return x - y;
    }

    public float multiply(float x, float y) {
        return x * y;
    }

    public float divide(float x, float y) {
        if (y != 0) {
            return x / y;
        } else
            return -1;
    }
}
package net.xqx.webservice;

public class HelloService {
    public String sayHelloNew() {
        return "hello";
    }

    public String sayHelloToPersonNew(String name) {
        if (name == null) {
            name = "nobody";
        }
        return "hello," + name;
    }

    public void updateData(String data) {
        System.out.println(data + " 已更新。");
    }
}
package net.xqx.webservice;

import net.xqx.bean.Persion;

public class PersionServer {
    public Persion persionInfo(String name,String sex, int age,String address) {  
        Persion persion = new Persion();  
        persion.setName(name);
        persion.setSex(sex);
        persion.setAge(age);
        persion.setAddress(address);
        System.out.println("PersionServer:"+persion.getName()+"/"+persion.getSex()+"/"+persion.getAge()+"/"+persion.getAddress());
        return persion;  
    }  
}
package net.xqx.bean;

public class Persion {
    private String name;
    private String sex;
    private int age;
    private String address;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

2、使用Eclipse的Web Service发布。

3、建立动态Web工程(Dynamic Web Project),工程名为webServiceClient作为测试客户端。

4、使用Eclipse的Web Service Client自动生成客户端代码。

5、编写测试类

package net.xqx.webservice;
 
import java.rmi.RemoteException;
import net.xqx.webservice.CalculateService;
import net.xqx.webservice.CalculateServiceProxy;
 
public class CalculateServiceTest {
   public static void main(String[] args) {
      try {
           CalculateService service = new CalculateServiceProxy();
            System.out.println(service.plus(1, 10));
            System.out.println(service.divide(1, 10));
            System.out.println(service.minus(1, 10));
            System.out.println(service.multiply(1, 10));
      } catch (RemoteException e) {
            e.printStackTrace();
      }
   }
}
package net.xqx.webservice;
 
import java.rmi.RemoteException;
import net.xqx.webservice.HelloService;
import net.xqx.webservice.HelloServiceProxy;
 
public class HelloServiceTest {
   public static void main(String[] args) {
      try {
            HelloService service = new HelloServiceProxy();
            System.out.println(service.sayHelloNew());
            System.out.println(service.sayHelloToPersonNew("达文西"));
            service.updateData("达文西");
      } catch (RemoteException e) {
            e.printStackTrace();
      }
   }
}
package net.xqx.webservice;

import java.rmi.RemoteException;

public class PersionServerTest {
    public static void main(String[] args) throws Exception{  
         try {
                PersionServer service = new PersionServerProxy();
                System.out.println(service.persionInfo("周迅", "女", 36, "熟女街").getName());
          } catch (RemoteException e) {
                e.printStackTrace();
          }
    }  
}

 6、自行编写测试客户端代码

package test;

import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class CalculateServiceTest {

    public static void main(String[] args) throws AxisFault {
        // TODO Auto-generated method stub

        // 使用RPC方式调用WebService
        RPCServiceClient serviceClient = new RPCServiceClient();
        Options options = serviceClient.getOptions();
        // 指定调用WebService的URL
        EndpointReference targetEPR = new EndpointReference("http://localhost:8080/webService/services/CalculateService");
        options.setTo(targetEPR);

        // 指定要调用的计算机器中的方法及WSDL文件的命名空间:net.xqx.webservice。
        QName opAddEntry = new QName("http://webservice.xqx.net","plus");//加法
        QName opAddEntryminus = new QName("http://webservice.xqx.net","minus");//减法
        QName opAddEntrymultiply = new QName("http://webservice.xqx.net","multiply");//乘法
        QName opAddEntrydivide = new QName("http://webservice.xqx.net","divide");//除法
        // 指定plus方法的参数值为两个,分别是加数和被加数
        Object[] opAddEntryArgs = new Object[] { 1,2 };
        // 指定plus方法返回值的数据类型的Class对象
        Class[] classes = new Class[] { float.class };
        // 调用plus方法并输出该方法的返回值
        System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]);
        System.out.println(serviceClient.invokeBlocking(opAddEntryminus,opAddEntryArgs, classes)[0]);
        System.out.println(serviceClient.invokeBlocking(opAddEntrymultiply,opAddEntryArgs, classes)[0]);
        System.out.println(serviceClient.invokeBlocking(opAddEntrydivide,opAddEntryArgs, classes)[0]);

    }
}
package test;
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class HelloServiceTest {
    public static void main(String args[]) throws AxisFault {
        // 使用RPC方式调用WebService
        RPCServiceClient serviceClient = new RPCServiceClient();
        Options options = serviceClient.getOptions();
        // 指定调用WebService的URL
        EndpointReference targetEPR = new EndpointReference("http://localhost:8080/webService/services/HelloService");
        options.setTo(targetEPR);
        
        // 指定要调用的sayHelloToPerson方法及WSDL文件的命名空间
        QName opAddEntry = new QName("http://webservice.xqx.net","sayHelloToPersonNew");
        // 指定sayHelloToPerson方法的参数值
        Object[] opAddEntryArgs = new Object[] { "xuwei" };
        // 指定sayHelloToPerson方法返回值的数据类型的Class对象
        Class[] classes = new Class[] { String.class };
        // 调用sayHelloToPerson方法并输出该方法的返回值
        System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]);
    }
}
package test;
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class HelloServiceTest {
    public static void main(String args[]) throws AxisFault {
        // 使用RPC方式调用WebService
        RPCServiceClient serviceClient = new RPCServiceClient();
        Options options = serviceClient.getOptions();
        // 指定调用WebService的URL
        EndpointReference targetEPR = new EndpointReference("http://localhost:8080/webService/services/HelloService");
        options.setTo(targetEPR);
        
        // 指定要调用的sayHelloToPerson方法及WSDL文件的命名空间
        QName opAddEntry = new QName("http://webservice.xqx.net","sayHelloToPersonNew");
        // 指定sayHelloToPerson方法的参数值
        Object[] opAddEntryArgs = new Object[] { "xuwei" };
        // 指定sayHelloToPerson方法返回值的数据类型的Class对象
        Class[] classes = new Class[] { String.class };
        // 调用sayHelloToPerson方法并输出该方法的返回值
        System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]);
    }
}

 

package test;
import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class HelloServiceTest2 {
    private RPCServiceClient serviceClient;
    private Options options;
    private EndpointReference targetEPR;

    public HelloServiceTest2(String endpoint) throws AxisFault {
        serviceClient = new RPCServiceClient();
        options = serviceClient.getOptions();
        targetEPR = new EndpointReference(endpoint);
        options.setTo(targetEPR);
    }

    public Object[] invokeOp(String targetNamespace, String opName,
            Object[] opArgs, Class<?>[] opReturnType) throws AxisFault,
            ClassNotFoundException {
        // 设定操作的名称
        QName opQName = new QName(targetNamespace, opName);
        // 设定返回值
        // Class<?>[] opReturn = new Class[] { opReturnType };
        // 操作需要传入的参数已经在参数中给定,这里直接传入方法中调用
        return serviceClient.invokeBlocking(opQName, opArgs, opReturnType);
    }

    /**
     * @param args
     * @throws AxisFault
     * @throws ClassNotFoundException
     */
    public static void main(String[] args) throws AxisFault,
            ClassNotFoundException {
        // TODO Auto-generated method stub
        final String endPointReference = "http://localhost:8080/webService/services/HelloService";
        final String targetNamespace = "http://webservice.xqx.net";
        HelloServiceTest2 client = new HelloServiceTest2(endPointReference);

        String opName = "sayHelloToPersonNew";
        Object[] opArgs = new Object[] { "My Friends" };
        Class<?>[] opReturnType = new Class[] { String[].class };

        Object[] response = client.invokeOp(targetNamespace, opName, opArgs,
                opReturnType);
        System.out.println(((String[]) response[0])[0]);
    }

}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。