前面两篇文章介绍了怎样通过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。
网站名称:ApacheCXF实战之三:传输Java对象
本文网址:http://www.csdahua.cn/qtweb/news43/217243.html
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网