新聞中心
這里有您想知道的互聯網營銷解決方案
ApacheCXF實戰(zhàn)之三:傳輸Java對象
前面兩篇文章介紹了怎樣通過CXF來構建最基本的Web Service,并且其中暴露的接口參數和返回值都是字符串,下面來看看一個稍微復雜一點的例子。

目前成都創(chuàng)新互聯公司已為上千余家的企業(yè)提供了網站建設、域名、網頁空間、成都網站托管、企業(yè)網站設計、交口網站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協力一起成長,共同發(fā)展。
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. 創(chuàng)建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. 創(chuàng)建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。
當前標題:ApacheCXF實戰(zhàn)之三:傳輸Java對象
URL網址:http://m.5511xx.com/article/dhcejhg.html


咨詢
建站咨詢
