ApacheCXF实战之三:传输Java对象

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

目前成都创新互联公司已为上千余家的企业提供了网站建设、域名、网页空间、成都网站托管、企业网站设计、交口网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

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

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

2. 创建Web Service接口类

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

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

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

4. 下面是Server端的代码

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

5. 下面是Client端的代码

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

6.测试

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

名称栏目:ApacheCXF实战之三:传输Java对象
本文网址:http://www.shufengxianlan.com/qtweb/news43/217243.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联