分享

CXF之动态客户端实例

 江江385 2013-01-30

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

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

服务接口:

[java:firstline[1]] view plaincopyprint?
  1. package test.cxf;
  2. import javax.jws.WebParam;
  3. import javax.jws.WebService;
  4. @WebService
  5. public interface HelloWorld {
  6. public Person sayPerson(
  7. @WebParam(name="personName")String personName,
  8. @WebParam(name="address")Address address);
  9. }

服务实现类:

[java:firstline[1]] view plaincopyprint?
  1. package test.cxf;
  2. import javax.jws.WebService;
  3. @WebService(endpointInterface="test.cxf.HelloWorld")
  4. public class HelloWorldImpl implements HelloWorld {
  5. public Person sayPerson(String personName, Address address) {
  6. Person p = new Person();
  7. p.setPersonName(personName);
  8. p.setAddress(address);
  9. p.setAge(30);
  10. System.out.println("server--" + p.getPersonName());
  11. System.out.println("server--" + p.getAge());
  12. System.out.println("server--" + p.getAddress().getAddress1()+":" + p.getAddress().getAddress2());
  13. return p;
  14. }
  15. }

对象 Person:

[java:firstline[1]] view plaincopyprint?
  1. package test.cxf;
  2. import java.io.Serializable;
  3. import javax.xml.bind.annotation.XmlAccessType;
  4. import javax.xml.bind.annotation.XmlAccessorType;
  5. @XmlAccessorType(XmlAccessType.FIELD)
  6. public class Person implements Serializable{
  7. private String personName;
  8. private Integer age;
  9. private Address address;
  10. public String getPersonName() {
  11. return personName;
  12. }
  13. public void setPersonName(String personName) {
  14. this.personName = personName;
  15. }
  16. public Integer getAge() {
  17. return age;
  18. }
  19. public void setAge(Integer age) {
  20. this.age = age;
  21. }
  22. public Address getAddress() {
  23. return address;
  24. }
  25. public void setAddress(Address address) {
  26. this.address = address;
  27. }
  28. }

对象Address:

[java:firstline[1]] view plaincopyprint?
  1. package test.cxf;
  2. import java.io.Serializable;
  3. import javax.xml.bind.annotation.XmlAccessType;
  4. import javax.xml.bind.annotation.XmlAccessorType;
  5. @XmlAccessorType(XmlAccessType.FIELD)
  6. public class Address implements Serializable{
  7. private String address1;
  8. private String address2;
  9. public String getAddress1() {
  10. return address1;
  11. }
  12. public void setAddress1(String address1) {
  13. this.address1 = address1;
  14. }
  15. public String getAddress2() {
  16. return address2;
  17. }
  18. public void setAddress2(String address2) {
  19. this.address2 = address2;
  20. }
  21. }

异常定义:

[java:firstline[1]] view plaincopyprint?
  1. package test.cxf;
  2. import javax.xml.bind.annotation.XmlAccessType;
  3. import javax.xml.bind.annotation.XmlAccessorType;
  4. import javax.xml.ws.WebFault;
  5. @XmlAccessorType(XmlAccessType.FIELD)
  6. @WebFault(name="DynamicClientException")
  7. public class DynamicClientException extends Exception {
  8. public DynamicClientException(){
  9. super();
  10. System.out.println("DynamicClientException!!!");
  11. }
  12. }

服务端配置:

[java:firstline[1]] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www./schema/beans"
  3. xmlns:xsi="http://www./2001/XMLSchema-instance"
  4. xmlns:jaxws="http://cxf./jaxws"
  5. xsi:schemaLocation="
  6. http://www./schema/beans http://www./schema/beans/spring-beans.xsd
  7. http://cxf./jaxws http://cxf./schemas/jaxws.xsd">
  8. <import resource="classpath:META-INF/cxf/cxf.xml" />
  9. <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
  10. <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
  11. <bean id="helloworldImpl" class="test.cxf.HelloWorldImpl"/>
  12. <jaxws:endpoint id="helloworldService" implementor="#helloworldImpl" address="/helloworld"/>
  13. </beans>

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

[java:firstline[1]] view plaincopyprint?
  1. package test.cxf;
  2. import java.lang.reflect.Method;
  3. import org.apache.cxf.endpoint.Client;
  4. import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
  5. public class ClientObj {
  6. public static void main(String[] args){
  7. JaxWsDynamicClientFactory dynamicClient = JaxWsDynamicClientFactory.newInstance();
  8. Client client = dynamicClient.createClient("http://localhost:8085/cxf-dynamicClientFactory/service/helloworld?wsdl");
  9. Object address = null;
  10. try {
  11. address = Thread.currentThread().getContextClassLoader().loadClass("test.cxf.Address").newInstance();
  12. Method m2 = address.getClass().getMethod("setAddress1",String.class);
  13. Method m3 = address.getClass().getMethod("setAddress2",String.class);
  14. m2.invoke(address, "qqqqqqq");
  15. m3.invoke(address, "ttttttt");
  16. Object[] rspArr = client.invoke("sayPerson", "fhd", address);
  17. if (null != rspArr && rspArr.length > 0) {
  18. Method m4 = rspArr[0].getClass().getMethod("getAge");
  19. Object obj1 = m4.invoke(rspArr[0]);
  20. System.out.println("ggggg:" + (Integer)obj1);
  21. Method m5 = rspArr[0].getClass().getMethod("getPersonName");
  22. Object obj2 = m5.invoke(rspArr[0]);
  23. System.out.println("vvvvv" + (String)obj2);
  24. Method m6 = rspArr[0].getClass().getMethod("getAddress");
  25. Object obj3 = m6.invoke(rspArr[0]);
  26. Method m7 = obj3.getClass().getMethod("getAddress1");
  27. Method m8 = obj3.getClass().getMethod("getAddress2");
  28. Object o1 = m7.invoke(obj3);
  29. Object o2 = m8.invoke(obj3);
  30. System.out.println("ssssss" + (String)o1 + "<<>>" + (String)o2);
  31. }
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. }

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多