Apache CXF實戰之三 傳輸Java對象__Java

來源:互聯網
上載者:User

本文連結:http://blog.csdn.net/kongxx/article/details/7527094

Apache CXF實戰之一 Hello World Web Service

Apache CXF實戰之二 整合Sping與Web容器

前面兩篇文章介紹了怎樣通過CXF來構建最基本的Web Service,並且其中暴露的介面參數和傳回值都是字串,下面來看看一個稍微複雜一點的例子。

1. 首先是一個普通的pojo對象,用來表示一個實體類

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介面類

package com.googlecode.garbagecan.cxfstudy.jaxws;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;@WebServicepublic interface CustomerService {    @WebMethod    @WebResult Customer findCustomer(@WebParam String id);}
3. 建立Web Service介面的實作類別

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端的代碼

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端的代碼

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。




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.