CXF 实现 WebService (1)
2014-08-05 23:00
246 查看
使用CXF实现一个WebService
简单的一个CXF例子
package org.howie.service;
import javax.jws.WebService;
@WebService //注解
public interface Hello {
public String sayHi(String name);
}
import org.howie.service.Hello;
//serviceName定义了WEBSERVICE服务的名称,客户端通过HelloWS 的getxxzzxxxport方法来获得该接口
@WebService(endpointInterface="org.howie.service.Hello",serviceName="HelloWS")
public class HelloImpl implements Hello{
@Override
public String sayHi(String name) {
System.out.println("111hi:"+name);
return "2222hi:"+name;
}
}
import javax.xml.ws.Endpoint;
import org.howie.service.Hello;
import org.howie.service.impl.HelloImpl;
public class MainTest {
public static void main(String[] args) {
String url="http://192.168.1.102:9090/helloService";
Hello hello=new HelloImpl();
//通过Endpoint的静态方法暴露webService 传入2个参数 该service的url,和服务接口
Endpoint.publish(url, hello);
}
}
如果见到一堆xml,那就成功了
如果是window系统 cmd 进入到客户端的项目中 cd cd cd 。。。到 cxf_client 下的org 下
输入 wsdl2java http://192.168.1.102:9090/helloService?wsdl 并执行
如果没报错,那就成功了,回到eclipse 刷新一下。会看到有一个新的包。
public static void main(String[] args) {
HelloWS helloWS=new HelloWS(); //这就是客户端定义的serviceName 这是一个工程类
Hello hello=helloWS.getHelloImplPort(); //通过getXXXPort() 获取服务类
String str=hello.sayHi("howie"); 调用服务类方法.
System.out.println(str);
}
}
简单的一个CXF例子
服务器端:
1.烤jar包 所需jar包如下 在cxf 的lib下找
2.建一个java project
new 一个 interfacepackage org.howie.service;
import javax.jws.WebService;
@WebService //注解
public interface Hello {
public String sayHi(String name);
}
3.实现该interface
import javax.jws.WebService;import org.howie.service.Hello;
//serviceName定义了WEBSERVICE服务的名称,客户端通过HelloWS 的getxxzzxxxport方法来获得该接口
@WebService(endpointInterface="org.howie.service.Hello",serviceName="HelloWS")
public class HelloImpl implements Hello{
@Override
public String sayHi(String name) {
System.out.println("111hi:"+name);
return "2222hi:"+name;
}
}
4.暴露服务
package org.howie.main;import javax.xml.ws.Endpoint;
import org.howie.service.Hello;
import org.howie.service.impl.HelloImpl;
public class MainTest {
public static void main(String[] args) {
String url="http://192.168.1.102:9090/helloService";
Hello hello=new HelloImpl();
//通过Endpoint的静态方法暴露webService 传入2个参数 该service的url,和服务接口
Endpoint.publish(url, hello);
}
}
5.测试 服务端
用浏览器 登陆 http://192.168.1.102:9090/helloService?wsdl如果见到一堆xml,那就成功了
客户端:
1.配置环境变量
将 apache-cxf-2.4.0\bin 的路径配置到path中2.获取服务端代码
建一个项目 cxf_client 定义一个action(暂且为空)如果是window系统 cmd 进入到客户端的项目中 cd cd cd 。。。到 cxf_client 下的org 下
输入 wsdl2java http://192.168.1.102:9090/helloService?wsdl 并执行
如果没报错,那就成功了,回到eclipse 刷新一下。会看到有一个新的包。
3.调用webservice
public class UserAction {public static void main(String[] args) {
HelloWS helloWS=new HelloWS(); //这就是客户端定义的serviceName 这是一个工程类
Hello hello=helloWS.getHelloImplPort(); //通过getXXXPort() 获取服务类
String str=hello.sayHi("howie"); 调用服务类方法.
System.out.println(str);
}
}
相关文章推荐
- webService 使用CXF 实现简单的helloworld
- java实现webservice的CXF框架和JDK1.6的冲突问题
- 使用CXF实现WebService,并在客户端实现动态调用
- webservice之CXF注解实现(一)
- WebService的实现——cxf
- Webservice实例-使用CXF实现
- CXF实现WebService
- 基于cxf实现的webservice,全程开发指南和笔记,以及代码
- 基于cxf实现的webservice,全程开发指南和笔记,以及代码
- webservice之CXF注解实现(三)
- CXF 实现webservice例子 集成spring
- CXF实现和Spring整合发布WebService
- cxf实现webservice简单插入,查询,和返回结构集
- 使用CXF实现Webservice的服务接口提供以及相关的客户端实现
- Tomcat下通过CXF实现不用注解发布WebService
- webService 使用CXF 实现简单的helloworld
- 使用CXF框架实现webservice的一个简单例子
- CXF实现WebService进行文件上传
- 初级篇---实现篇---WebService---CXF---Spring V0.0.1
- Tomcat下通过CXF实现不用注解发布WebService