cxf 开发客户端

开发工具准备:

1apache-cxf-2.2.6.zip

2spring-ws-1.5.8.zip

3eclipse-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文件,内容如下:

<?xmlversion="1.0"encoding="UTF-8"?>

 

<beansxmlns="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">

 

    <beanid="client"class="demo.spring.HelloWorld"

      factory-bean="clientFactory"factory-method="create"/>

   

    <beanid="clientFactory"class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">

      <propertyname="serviceClass"value="demo.spring.HelloWorld"/>

      <propertyname="address"value="http://localhost:8080/WebService_CXF_Host/service/HelloWorld"/>

    </bean>

 

      <jaxws:clientid="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);

       }

}

 

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

你可能感兴趣的:(CXF)