Apache CXF中如何传输Java对象
短信预约 -IT技能 免费直播动态提醒
这篇文章将为大家详细讲解有关Apache CXF中如何传输Java对象,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
怎样通过CXF来构建最基本的Web Service,并且其中暴露的接口参数和返回值都是字符串,下面来看看一个稍微复杂一点的例子。
首先是一个普通的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); } }
创建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); }
创建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; } }
下面是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(); } }
下面是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(); } } } }
测试
首先运行MyServer类,然后运行MyClient类来验证Web Service。
关于Apache CXF中如何传输Java对象就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341