您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Apache CXF中如何傳輸Java對象,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
怎樣通過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; @WebService public 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。
關于Apache CXF中如何傳輸Java對象就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。