现在的位置: 首页 > 综合 > 正文

WebService CXF学习(进阶篇3):对象传递

2017年09月13日 ⁄ 综合 ⁄ 共 6513字 ⁄ 字号 评论关闭

 前面几节都是讲一些理论知识,现在又用一个例子来说明一下,这一节我们就CXF框架对象传递进行讲解。
    第一步:创建传输对象Customer

Java代码
复制代码
 收藏代码
  1.    @XmlRootElement(name="Customer")   
  2.    @XmlAccessorType(XmlAccessType.FIELD)   
  3.    @XmlType(propOrder = {"name","age"})   
  4.    public class Customer {   
  5.   
  6. private int age;   
  7. private String name;   
  8.   
  9. public int getAge() {   
  10.     return age;   
  11. }   
  12.   
  13. public void setAge(int age) {   
  14.     this.age = age;   
  15. }   
  16.   
  17. public String getName() {   
  18.     return name;   
  19. }   
  20.   
  21. public void setName(String name) {   
  22.     this.name = name;   
  23. }   
  24.   
  25.    }  

    @XmlRootElement-指定XML根元素名称(可选)
    @XmlAccessorType-控制属性或方法序列化
    四种方案:
    FIELD-对每个非静态,非瞬变属性JAXB工具自动绑定成XML,除非注明XmlTransient
    NONE-不做任何处理
    PROPERTY-对具有set/get方法的属性进行绑定,除非注明XmlTransient
    PUBLIC_MEMBER -对有set/get方法的属性或具有共公访问权限的属性进行绑定,除非注
    明XmlTransient
    @XmlType-映射一个类或一个枚举类型成一个XML Schema类型

   第二步:创建WebService接口
  

Java代码
复制代码
 收藏代码
  1.   @WebService  
  2.   public interface HelloService {   
  3.   
  4. public void save(Customer c1,Customer c2);   
  5.   
  6. public void test(String args);   
  7.   
  8. public Customer get(int id);   
  9.  }   
  10.   
  11.     

   每三步:创建WebService接口实现类
  

Java代码
复制代码
 收藏代码
  1.   @WebService  
  2.   public class HelloServiceImpl implements HelloService {   
  3.   
  4. public void save(Customer c1, Customer c2) {   
  5.   
  6.     System.out.println(c1.getAge()+"---"+c2.getAge());   
  7.     System.out.println(c1.getName()+"---"+c2.getName());   
  8. }   
  9.   
  10. public void test(String args) {   
  11.     System.out.println(args);   
  12.        
  13. }   
  14.   
  15. public Customer get(int id) {   
  16.     Customer cus = new Customer();   
  17.     cus.setAge(100);   
  18.     cus.setName("Josen");   
  19.     return cus;   
  20. }   
  21.   
  22.   
  23.   
  24.    }   
  25.     

   第四步:创建服务端
  

Java代码
复制代码
 收藏代码
  1.  public class SoapServer {   
  2.   
  3. ublic static void main(String[] args){   
  4. //两种方法,任选一种发布WebService接口   
  5.               //Endpoint.publish("http://localhost:8080/helloService", new 
      
  6.                HelloServiceImpl());   
  7. JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();   
  8. factory.setAddress("http://localhost:8080/helloService");   
  9. factory.setServiceClass(HelloServiceImpl.class);   
  10.               factory.getInInterceptors().add(new LoggingInInterceptor());         
  11. factory.getOutInterceptors().add(new LoggingOutInterceptor());   
  12. factory.create();   
  13.   
  14.   }   
  15.    

  第五步:创建客户端
 

Java代码
复制代码
 收藏代码
  1.  public class SoapClient {   
  2.   
  3. public static void main(String[] args){   
  4.     JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();   
  5.     factory.setAddress("http://localhost:8080/helloService");   
  6.     factory.setServiceClass(HelloService.class);   
  7.                factory.setServiceClass(HelloServiceImpl.class);   
  8.                factory.getInInterceptors().add(new LoggingInInterceptor());   
  9.     HelloService service = (HelloService)factory.create();   
  10.        
  11.     Customer c1 = new Customer();   
  12.     c1.setAge(1);   
  13.     c1.setName("aaa");   
  14.        
  15.     Customer c2 = new Customer();   
  16.     c2.setAge(2);   
  17.     c2.setName("bbb");   
  18.        
  19.     service.save(c1, c2);   
  20.     service.test("aaaaaaaaaaaaa");   
  21. }   
  22.  }   
  23.    

  最后,测试程序
  运行服务端程序,在浏览器地址栏输入http://localhost:8080/helloService?wsdl查看接口是否发布成功。成功则运行一下客户端程序,看看对象传输是否成功。

   现在我们来分析一下控制打印的日志信息。

引用
信息: Inbound Message
----------------------------
ID: 1
Address: /HelloWorld
Encoding: UTF-8
Content-Type: text/xml; charset=UTF-8
Headers: {content-type=[text/xml; charset=UTF-8], connection=[keep-alive], Host=[localhost:9000], Content-Length=[184], SOAPAction=[""], User-Agent=[Apache CXF 2.2.2], Content-Type=[text/xml; charset=UTF-8], Accept=[*/*], Pragma=[no-cache], Cache-Control=[no-cache]}

Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:say xmlns:ns1="http://client.itdcl.com/"><text> Josen</text></ns1:say></soap:Body></soap:Envelope>

--------------------------------------
2010-1-9 20:41:56 org.apache.cxf.interceptor.LoggingOutInterceptor$LoggingCallback onClose

信息: Outbound Message
---------------------------
ID: 1
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><text xmlns="http://client.itdcl.com/">hi Josen</text></soap:Header><soap:Body><ns1:sayResponse xmlns:ns1="http://client.itdcl.com/"></ns1:sayResponse></soap:Body></soap:Envelope>

--------------------------------------
2010-01-09 20:41:56.578::INFO:  seeing JVM BUG(s) - cancelling interestOps==0

     当客户端向服器发送请求时,服务端LoggingInInterceptor拉截客户端发送过来的SOAP消息,如下:

引用
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns1:sayHi xmlns:ns1="http://client.itdcl.com/">
<text>Josen</text>
</ns1:sayHi>
</soap:Body>
</soap:Envelope>

     客户端将请求信息封闭在<soap:Body></soap:Body>中,当然也可以将其放到<soap:Header></soap:Header>,只要在@WebParam中的header设置成true,默认为false;

     服务器接到请求之后,响应客户端。同样以SOAP形式将信息封装好发回客户端,SOAP信息如下:

引用
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<text xmlns="http://client.itdcl.com/">hi Josen</text>
</soap:Header>
<soap:Body>
<ns1:sayResponse xmlns:ns1="http://client.itdcl.com/"></ns1:sayResponse>
</soap:Body>
</soap:Envelope>

抱歉!评论已关闭.