CXF开发WebService客户端

开发必备
1.apache-cxf-2.2.6
2.spring-ws-1.5.8
3.eclipse-jee-galileo-SR1-win32

开发步骤:
一、新建一个普通的java工程,名字叫WebService_CXF_Client
二、导入apache-cxf-2.2.6 及 spring-ws-1.5.8 下的jar包
三、启动《CXF开发WebService服务器端》所开发的WebService服务器
四、进入apache-cxf-2.2.6/bin文件夹内,运行
wsdl2java.bat http://localhost:8080/WebService_CXF_Host/service/HelloWorld?wsdl
将会在bin文件夹内生成一个demo文件夹
注意,可以查看demo.sprint文件夹内的HelloWorld.java接口,定义的aryMyObjects方法的返回值是List<MyObject>类型,尽管在服务器端定义接口的返回值类型是数组.
五、将整个demo文件夹剪切到WebService_CXF_Client工程的src目录下
六、在src目录下新建一个client-beans.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <bean id="client" class="demo.spring.HelloWorld"
      factory-bean="clientFactory" factory-method="create"/>
  
    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
      <property name="serviceClass" value="demo.spring.HelloWorld"/>
      <property name="address" value="http://localhost:8080/WebService_CXF_Host/service/HelloWorld"/>
    </bean>

      <jaxws:client id="client2"
                  serviceClass="demo.spring.HelloWorld"
                  address="http://localhost:8080/WebService_CXF_Host/service/HelloWorld" />
</beans>

注意:client 与 client2 是两种访问webservice的写法,效果一样。

七、新建含main方法的Client.java类,内容中下:

import java.util.ArrayList;
import java.util.List;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import demo.spring.HelloWorld;
import demo.spring.MyObject;


public final class Client {

    private Client() {
    }

    public static void main(String args[]) throws Exception {
        // START SNIPPET: client
        ClassPathXmlApplicationContext context
            = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});

        HelloWorld client = (HelloWorld)context.getBean("client2");

        System.out.println("begin stringInt method");
        int num=999;
        String text="i love you";
        String response = client.stringInt(text, num);
        System.out.println("Response: " + response);
      
        System.out.println("begin aryMyObjects method");
        List<MyObject> lstSource=new ArrayList<MyObject>();
        MyObject myObject1=new MyObject();
        myObject1.setId(0);
        myObject1.setName("000");
        MyObject myObject2=new MyObject();
        myObject2.setId(1);
        myObject2.setName("111");
        lstSource.add(myObject1);
        lstSource.add(myObject2);
      
        List<MyObject> lstResponse=client.aryMyObjects(lstSource);
        for(int i=0;i<lstResponse.size();i++){
               System.out.println("id: "+lstResponse.get(i).getId()+"   name: "+lstResponse.get(i).getName());
        }
      
        System.exit(0);
       }
}

至此,客户端也成功开发了。

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