新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
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í)體類
- 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接口的實(shí)現(xiàn)類
- 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.測(cè)試
首先運(yùn)行MyServer類,然后運(yùn)行MyClient類來(lái)驗(yàn)證Web Service。
文章標(biāo)題:ApacheCXF實(shí)戰(zhàn)之三:傳輸Java對(duì)象
文章URL:http://fisionsoft.com.cn/article/dhcejhg.html


咨詢
建站咨詢
