日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯網營銷解決方案
ApacheCXF實戰(zhàn)之三:傳輸Java對象

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

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

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. 創(chuàng)建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. 創(chuàng)建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實戰(zhàn)之三:傳輸Java對象
URL網址:http://m.5511xx.com/article/dhcejhg.html