Apache CXF实战之三:传输Java对象

开发 后端
前面两篇文章介绍了怎样通过CXF来构建最基本的Web Service,并且其中暴露的接口参数和返回值都是字符串,下面来看看一个稍微复杂一点的例子。

前面两篇文章介绍了怎样通过CXF来构建最基本的Web Service,并且其中暴露的接口参数和返回值都是字符串,下面来看看一个稍微复杂一点的例子。

1. 首先是一个普通的pojo对象,用来表示一个实体类

  1. package com.googlecode.garbagecan.cxfstudy.jaxws;  
  2.  
  3. import java.util.Date;  
  4.  
  5. public class Customer {  
  6.     private String id;  
  7.     private String name;  
  8.     private Date birthday;  
  9.     public String getId() {  
  10.         return id;  
  11.     }  
  12.     public void setId(String id) {  
  13.         this.id = id;  
  14.     }  
  15.     public String getName() {  
  16.         return name;  
  17.     }  
  18.     public void setName(String name) {  
  19.         this.name = name;  
  20.     }  
  21.     public Date getBirthday() {  
  22.         return birthday;  
  23.     }  
  24.     public void setBirthday(Date birthday) {  
  25.         this.birthday = birthday;  
  26.     }  
  27.     @Override 
  28.     public String toString() {  
  29.         return org.apache.commons.lang.builder.ToStringBuilder.reflectionToString(this);  
  30.     }  

2. 创建Web Service接口类

  1. package com.googlecode.garbagecan.cxfstudy.jaxws;  
  2.  
  3. import javax.jws.WebMethod;  
  4. import javax.jws.WebParam;  
  5. import javax.jws.WebResult;  
  6. import javax.jws.WebService;  
  7.  
  8. @WebService 
  9. public interface CustomerService {  
  10.     @WebMethod 
  11.     @WebResult Customer findCustomer(@WebParam String id);  

3. 创建Web Service接口的实现类

  1. package com.googlecode.garbagecan.cxfstudy.jaxws;  
  2.  
  3. import java.util.Calendar;  
  4.  
  5. public class CustomerServiceImpl implements CustomerService {  
  6.  
  7.     public Customer findCustomer(String id) {  
  8.         Customer customer = new Customer();  
  9.         customer.setId("customer_" + id);  
  10.         customer.setName("customer_name");  
  11.         customer.setBirthday(Calendar.getInstance().getTime());  
  12.         return customer;  
  13.     }  

4. 下面是Server端的代码

  1. package com.googlecode.garbagecan.cxfstudy.jaxws;  
  2.  
  3. import javax.xml.ws.Endpoint;  
  4.  
  5. import org.apache.cxf.interceptor.LoggingInInterceptor;  
  6. import org.apache.cxf.interceptor.LoggingOutInterceptor;  
  7. import org.apache.cxf.jaxws.JaxWsServerFactoryBean;  
  8.  
  9. public class MyServer {  
  10.       
  11.     private static final String address = "http://localhost:9000/ws/jaxws/customerService";  
  12.       
  13.     public static void main(String[] args) throws Exception {  
  14.         // http://localhost:9000/ws/jaxws/customerService?wsdl  
  15.         JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();  
  16.         factoryBean.getInInterceptors().add(new LoggingInInterceptor());  
  17.         factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());  
  18.  
  19.         factoryBean.setServiceClass(CustomerServiceImpl.class);  
  20.         factoryBean.setAddress(address);  
  21.         factoryBean.create();  
  22.     }  

5. 下面是Client端的代码

  1. package com.googlecode.garbagecan.cxfstudy.jaxws;  
  2.  
  3. import java.net.SocketTimeoutException;  
  4.  
  5. import javax.xml.ws.WebServiceException;  
  6.  
  7. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  8.  
  9. public class MyClient {  
  10.     public static void main(String[] args) throws Exception {  
  11.         JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();  
  12.         factoryBean.setAddress("http://localhost:9000/ws/jaxws/customerService");  
  13.         factoryBean.setServiceClass(CustomerService.class);  
  14.         Object obj = factoryBean.create();  
  15.  
  16.         CustomerService customerService = (CustomerService) obj;  
  17.         try {  
  18.             Customer customer = customerService.findCustomer("123");  
  19.             System.out.println("Customer: " + customer);  
  20.         } catch(Exception e) {  
  21.             if (e instanceof WebServiceException   
  22.                     && e.getCause() instanceof SocketTimeoutException) {  
  23.                 System.err.println("This is timeout exception.");  
  24.             } else {  
  25.                 e.printStackTrace();  
  26.             }  
  27.         }  
  28.     }  
  29. }  

6.测试

首先运行MyServer类,然后运行MyClient类来验证Web Service。

原文链接:http://blog.csdn.net/kongxx/article/details/7527094

【系列文章】

  1. Apache CXF实战之五:压缩Web Service数据
  2. Apache CXF实战之四:构建RESTful Web Service
  3. Apache CXF实战之三:传输Java对象
  4. Apache CXF实战之二:集成Sping与Web容器
  5. Apache CXF实战之一:Hello World Web Service
责任编辑:林师授 来源: kongxx的博客
相关推荐

2012-02-15 10:37:38

JavaJava Socket

2012-05-07 14:15:41

ApacheCXFJava

2012-05-03 10:55:51

ApacheMINAJava

2012-05-03 11:51:59

ApacheCXFJava

2012-05-03 11:21:58

ApacheCXFJava

2012-05-03 11:43:32

ApacheCXFRESTful

2012-05-03 11:30:04

ApacheCXFJava

2012-02-15 10:40:37

JavaJava Socket

2012-05-07 14:08:20

ApacheCXFJava

2013-12-11 10:40:31

虚拟化实战Cluster

2013-05-28 09:33:47

虚拟化虚拟化存储

2013-06-08 11:10:36

虚拟化虚拟化存储

2012-02-15 10:44:20

JavaJava Socket

2019-09-18 18:32:29

前端javascriptoop

2012-03-15 17:18:33

JavaHashMap

2009-06-22 13:33:00

Apache CXF2JAX-WS2.0

2015-10-30 15:30:54

LevelDBSSTableSybase

2011-12-07 10:56:29

ApacheMakeJava

2011-06-24 16:26:20

SEO

2012-05-15 13:40:44

JavaCXF.NET
点赞
收藏

51CTO技术栈公众号