Apache CXF实战之三 传输Java对象

23 篇文章 1 订阅
8 篇文章 0 订阅

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

Apache CXF实战之一 Hello World Web Service

Apache CXF实战之二 集成Sping与Web容器

前面两篇文章介绍了怎样通过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. }
package com.googlecode.garbagecan.cxfstudy.jaxws;

import java.util.Date;

public class Customer {
    private String id;
    private String name;
    private Date birthday;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    @Override
    public String toString() {
        return org.apache.commons.lang.builder.ToStringBuilder.reflectionToString(this);
    }
}
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. }
package com.googlecode.garbagecan.cxfstudy.jaxws;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService
public interface CustomerService {
    @WebMethod
    @WebResult Customer findCustomer(@WebParam String id);
}
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. }
package com.googlecode.garbagecan.cxfstudy.jaxws;

import java.util.Calendar;

public class CustomerServiceImpl implements CustomerService {

    public Customer findCustomer(String id) {
        Customer customer = new Customer();
        customer.setId("customer_" + id);
        customer.setName("customer_name");
        customer.setBirthday(Calendar.getInstance().getTime());
        return customer;
    }
}
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. private static final String address = "http://localhost:9000/ws/jaxws/customerService";
  8. public static void main(String[] args) throws Exception {
  9. // http://localhost:9000/ws/jaxws/customerService?wsdl
  10. JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
  11. factoryBean.getInInterceptors().add(new LoggingInInterceptor());
  12. factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
  13. factoryBean.setServiceClass(CustomerServiceImpl.class);
  14. factoryBean.setAddress(address);
  15. factoryBean.create();
  16. }
  17. }
package com.googlecode.garbagecan.cxfstudy.jaxws;

import javax.xml.ws.Endpoint;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class MyServer {
    
    private static final String address = "http://localhost:9000/ws/jaxws/customerService";
    
    public static void main(String[] args) throws Exception {
        // http://localhost:9000/ws/jaxws/customerService?wsdl
        JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
        factoryBean.getInInterceptors().add(new LoggingInInterceptor());
        factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());

        factoryBean.setServiceClass(CustomerServiceImpl.class);
        factoryBean.setAddress(address);
        factoryBean.create();
    }
}
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. }
package com.googlecode.garbagecan.cxfstudy.jaxws;

import java.net.SocketTimeoutException;

import javax.xml.ws.WebServiceException;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class MyClient {
    public static void main(String[] args) throws Exception {
        JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
        factoryBean.setAddress("http://localhost:9000/ws/jaxws/customerService");
        factoryBean.setServiceClass(CustomerService.class);
        Object obj = factoryBean.create();

        CustomerService customerService = (CustomerService) obj;
        try {
            Customer customer = customerService.findCustomer("123");
            System.out.println("Customer: " + customer);
        } catch(Exception e) {
            if (e instanceof WebServiceException 
                    && e.getCause() instanceof SocketTimeoutException) {
                System.err.println("This is timeout exception.");
            } else {
                e.printStackTrace();
            }
        }
    }
}
6.测试

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值