本文链接:http://blog.csdn.net/kongxx/article/details/7527094
Apache CXF实战之一 Hello World Web Service
Apache CXF实战之二 集成Sping与Web容器
前面两篇文章介绍了怎样通过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。
分享到:
相关推荐
应用、 传输协议 (Transports) 应用, 并隆重推出了 Apache CXF框架如何实现 RESTful 服务、如何支持动态语言和 WS-* 规范 等,另外,还包括 Apache CXF 框架一些高级功能的用法。最后,本书还描述了 Apache CXF ...
Apache CXF Apache CXF Apache CXF Apache CXF Apache CXF Apache CXF Apache CXF Apache CXF Apache CXF
覆盖以下内容:基于JAX-WS规范和CXF自身的前端模式实现,CXF支持的数据绑定(DataBindings),CXF支持的WSDL绑定,CXF支持的传输协议绑定。CXF的调式、配置、日志、发布和工具。CXF实现RESTful服务。CXF对WS-* 的...
JAVA7和JAVA8对应CXF资源 WebService CXF 用了一天时间找,官网打不开,国内要积分,下下来又永不了。最后终于搞到手,上传上来分享给大家。 jdk版本 CXF版本 java 9及以上 3.3.x java 8 3.x java 7 2.2x --- ...
apache cxf 2.1.3 的用户手册
apache cxf 框架wsdl2java命令的使用。 -encoding是指定编码类型; -p 指定包名 -d 指定生成目录 -all生成服务端和客户端代码 -client 生成客户端代码 -server生成服务端代码 wsdl2java -...
webserviceApache CXF java springboot利用Apache CXF创建webserice接口 Apache CXF 核心架构是以BUS为核心,整合其他组件。 * Bus是CXF的主干, 为共享资源提供一个可配置的场所,作用类似于Spring的...
Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且...
apache cxf 3.1.8 java web service 开源框架
Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。
apache CXF wsdl2java工具使用说明
java通过cxf实现webservice所需jar包。java通过cxf实现webservice所需jar包。
Apache CXF Web Service Development
Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且...
apache cxf 一个helloworld的例子
Apache CXF Web Service Development Developing+Web+Services+with+Apache+CXF+and+Axis2+(3rd+Edition)
apache-cxf将wsdl生成java文件
apache-cxf 2.2.8 支持webservice 反正生成服务端代码,附带一个文本文件。希望有需要的小伙伴可以下载来看。