Apache CXF 是一个开源的 Services 框架
下载 apache-cxf-2.2.10 ,apache-cxf-2.2.10-src
1:new Java Project
2:apache-cxf-2.2.10\apache-cxf-2.2.10\lib中的jar 导入项目
3: 创建接口
- package com.apache.cxf.demo;
- import javax.jws.WebService;
- @WebService //标注最终被XFire读取后进行分析后会进一步处理成Web服务
- public interface CxfDemoInterFace {
- public String sayHelloWord(String name);
- }
实现类 package com.apache.cxf.demo;
- import javax.jws.WebService;
- @WebService
- public class CxfDemoImpl implements CxfDemoInterFace{
- @Override
- public String sayHelloWord(String name) {
- // TODO Auto-generated method stub
- System.out.println("Say HelloWord");
- return "Hello "+ name;
- }
- }
4:创建WebService的启动类
- package javacode.apacheCxf.demo;
- import org.apache.cxf.endpoint.Server;
- import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
- /**
- * 服务器
- * @author playjava
- *
- */
- public class StartWebServer {
- public static void main(String[] args) {
- JaxWsServerFactoryBean jwf = new JaxWsServerFactoryBean();
- jwf.setServiceClass(CxfDemoImpl.class); //设置webservice的具体实现类
- jwf.setAddress("http://localhost:8080/CxfDemo"); //设置地址
- Server server = jwf.create();
- server.start();
- }
- }
执行Main方法控制台输出显示:
- 信息: jetty-6.1.21
- 2010-10-10 13:36:43 org.mortbay.log.Slf4jLog info
- 信息: Started SelectChannelConnector@localhost:8080
地址栏访问设置路径:http://localhost:8080/CxfDemo显示如下
表示 WebServer启动成功
5:创建客户端
- package javacode.apacheCxf.demo;
- import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
- /**
- * 客户端
- * @author playjava
- *
- */
- public class Client {
- public static void main(String[] args) {
- JaxWsProxyFactoryBean jwpf = new JaxWsProxyFactoryBean();
- jwpf.setAddress("http://localhost:8080/CxfDemo");
- jwpf.setServiceClass(CxfDemo.class);
- CxfDemo cxfd = (CxfDemo)jwpf.create();
- System.out.println(cxfd.sayHelloWord("JE"));
- }
- }
执行Main方法客户端控制台输出显示:
- Hello JE