转自:http://blog.csdn.net/fhd001/article/details/5779411

  通常创建一个web服务的客户端包括SEI的java接口和客户端输入输出应用的一些java类,这不总是可取的或实用的。

  CXF支持不同的选择,允许一个应用连接到服务而不使用SEI和一些数据类。本页面介绍CXF的动态客户端工具,有了动态客户端,CXF就可以在运行时生成SEI和一些Bean类,也允许你通过API调用操作,即采取对象或使用反射来应用代理。

服务接口:

    package test.cxf;  
    import javax.jws.WebParam;  
    import javax.jws.WebService;  
    @WebService  
    public interface HelloWorld {  
        public Person sayPerson(  
                @WebParam(name="personName")String personName,  
                @WebParam(name="address")Address address);  
    }  

服务实现类:

    package test.cxf;  
    import javax.jws.WebService;  
    @WebService(endpointInterface="test.cxf.HelloWorld")  
    public class HelloWorldImpl implements HelloWorld {  
        public Person sayPerson(String personName, Address address) {  
              
            Person p = new Person();  
            p.setPersonName(personName);  
            p.setAddress(address);  
            p.setAge(30);  
              
            System.out.println("server--" + p.getPersonName());  
            System.out.println("server--" + p.getAge());  
            System.out.println("server--" + p.getAddress().getAddress1()+":" + p.getAddress().getAddress2());  
            return p;  
        }  
    }  

对象 Person:

    package test.cxf;  
    import java.io.Serializable;  
    import javax.xml.bind.annotation.XmlAccessType;  
    import javax.xml.bind.annotation.XmlAccessorType;  
    @XmlAccessorType(XmlAccessType.FIELD)  
    public class Person implements Serializable{  
        private String personName;  
        private Integer age;  
        private Address address;  
        public String getPersonName() {  
            return personName;  
        }  
        public void setPersonName(String personName) {  
            this.personName = personName;  
        }  
        public Integer getAge() {  
            return age;  
        }  
        public void setAge(Integer age) {  
            this.age = age;  
        }  
        public Address getAddress() {  
            return address;  
        }  
        public void setAddress(Address address) {  
            this.address = address;  
        }  
    }  

对象Address:

    package test.cxf;  
    import java.io.Serializable;  
    import javax.xml.bind.annotation.XmlAccessType;  
    import javax.xml.bind.annotation.XmlAccessorType;  
    @XmlAccessorType(XmlAccessType.FIELD)  
    public class Address implements Serializable{  
        private String address1;  
        private String address2;  
        public String getAddress1() {  
            return address1;  
        }  
        public void setAddress1(String address1) {  
            this.address1 = address1;  
        }  
        public String getAddress2() {  
            return address2;  
        }  
        public void setAddress2(String address2) {  
            this.address2 = address2;  
        }  
    }  

异常定义:

    package test.cxf;  
    import javax.xml.bind.annotation.XmlAccessType;  
    import javax.xml.bind.annotation.XmlAccessorType;  
    import javax.xml.ws.WebFault;  
    @XmlAccessorType(XmlAccessType.FIELD)  
    @WebFault(name="DynamicClientException")  
    public class DynamicClientException extends Exception {  
        public DynamicClientException(){  
            super();  
            System.out.println("DynamicClientException!!!");  
        }  
    }  

服务端配置:

    <?xml version="1.0" encoding="UTF-8"?>  
    <beans xmlns="http://www.springframework.org/schema/beans"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
        xmlns:jaxws="http://cxf.apache.org/jaxws"  
        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">  
        <import resource="classpath:META-INF/cxf/cxf.xml" />  
        <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
        <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
          
        <bean id="helloworldImpl" class="test.cxf.HelloWorldImpl"/>  
        <jaxws:endpoint id="helloworldService" implementor="#helloworldImpl" address="/helloworld"/>  
    </beans>  

客户端调用-----关键就在这里:

    package test.cxf;  
    import java.lang.reflect.Method;  
    import org.apache.cxf.endpoint.Client;  
    import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;  
    public class ClientObj {  
        public static void main(String[] args){  
            JaxWsDynamicClientFactory dynamicClient = JaxWsDynamicClientFactory.newInstance();  
            Client client = dynamicClient.createClient("http://localhost:8085/cxf-dynamicClientFactory/service/helloworld?wsdl");  
            Object address = null;  
            try {  
                address = Thread.currentThread().getContextClassLoader().loadClass("test.cxf.Address").newInstance();  
                Method m2 = address.getClass().getMethod("setAddress1",String.class);  
                Method m3 = address.getClass().getMethod("setAddress2",String.class);  
                m2.invoke(address, "qqqqqqq");  
                m3.invoke(address, "ttttttt");  
                Object[] rspArr = client.invoke("sayPerson", "fhd", address);  
                if (null != rspArr && rspArr.length > 0) {  
                      
                    Method m4 = rspArr[0].getClass().getMethod("getAge");  
                    Object obj1 = m4.invoke(rspArr[0]);  
                    System.out.println("ggggg:" + (Integer)obj1);  
                      
                    Method m5 = rspArr[0].getClass().getMethod("getPersonName");  
                    Object obj2 = m5.invoke(rspArr[0]);  
                    System.out.println("vvvvv" + (String)obj2);  
                      
                    Method m6 = rspArr[0].getClass().getMethod("getAddress");  
                    Object obj3 = m6.invoke(rspArr[0]);  
                    Method m7 = obj3.getClass().getMethod("getAddress1");  
                    Method m8 = obj3.getClass().getMethod("getAddress2");  
                    Object o1 = m7.invoke(obj3);  
                    Object o2 = m8.invoke(obj3);  
                    System.out.println("ssssss" + (String)o1 + "<<>>" + (String)o2);  
                }  
                  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
    }  

你可能会问自己下面这个问题:“类名'com.acme.Person'从哪里来”?是通过运行"wsdl2java'并审查结果来得到类名。