日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
ApacheCXF實(shí)戰(zhàn)之三:傳輸Java對(duì)象

前面兩篇文章介紹了怎樣通過(guò)CXF來(lái)構(gòu)建最基本的Web Service,并且其中暴露的接口參數(shù)和返回值都是字符串,下面來(lái)看看一個(gè)稍微復(fù)雜一點(diǎn)的例子。

目前成都創(chuàng)新互聯(lián)公司已為上千余家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁(yè)空間、成都網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、交口網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

1. 首先是一個(gè)普通的pojo對(duì)象,用來(lái)表示一個(gè)實(shí)體類(lèi)

 
 
 
  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. 創(chuàng)建Web Service接口類(lèi)

 
 
 
  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. 創(chuàng)建Web Service接口的實(shí)現(xiàn)類(lèi)

 
 
 
  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.測(cè)試

首先運(yùn)行MyServer類(lèi),然后運(yùn)行MyClient類(lèi)來(lái)驗(yàn)證Web Service。


新聞名稱(chēng):ApacheCXF實(shí)戰(zhàn)之三:傳輸Java對(duì)象
分享路徑:http://www.dlmjj.cn/article/dhcejhg.html