CXF框架构建和开发 Services
Apache CXF 是一个开源的 Services 框架,CXF 帮助您来构建和开发 Services 这些 Services 可以支持多种协议,比如:SOAP、POST/HTTP、RESTful HTTP CXF 大大简化了 Service可以天然地和 Spring 进行无缝集成。
ServerFactoryBean来发布web服务
服务类代码如下:
// 注解是无效的
@WebService(name="Hello",targetNamespace="http://icast.cn")
public class HelloWorld {
public String sayHi(String name) {
return "hello---->" + name;
}
}
发布类代码如下:
public static void main(String[] args) {
// 发布服务的类, 类似Endpoint
ServerFactoryBean serverFactoryBean=new ServerFactoryBean();
// 注册服务器地址和端口
serverFactoryBean.setAddress("http://127.0.0.1:9999/hello");
// 注册哪个类提供服务
serverFactoryBean.setServiceBean(new HelloWorld());
// 发布一个cxf服务
serverFactoryBean.create();
// 一分钟有服务终止
Thread.sleep(1 * 60 * 1000);
// 正常退出程序
System.exit(0);
}
ServerFactoryBean注意事项:
这种方式没有添加webService注解,也就是说没有注解也可以发布webService服务,但是这种方式不是很规范,比如我们不可以通过注解的方式来修改WSDL的标签信息,
CXF与Spring集成发布WebService
配置开发环境:
l 建立一个web项目
l 准备所有jar包,将CXF_HOME\lib项目下的所有jar包,全部都拷贝新项目的lib目录下.其中里面已经包含了Sring3.0的jar包 其中jetty 服务器的包可以不要.因为我们要部署的tomcat服务器中了
l 在web.xml中配置cxf的核心servlet,CXFServlet
l 此配置文件的作用类 拦截/ws/*的所有请求 类似Struts2的过滤器
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
通过Spring配置文件发布服务
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 这样配置自身的服务也可以使用 -->
<bean id="userImpl" class="cn.loaderman.i.cxf.spring.ws.UserImpl" />
<!-- id:逻辑名 serviceClass=服务接口类 address:调用的路径 http://localhost:8888/项目名/ws/hello?wsdl> -->
<jaxws:server id="userService" serviceClass="cn.loaderman.i.cxf.spring.ws.IUser" address="/hello">
<jaxws:serviceBean>
<ref bean="userImpl" />
</jaxws:serviceBean>
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
</jaxws:outInterceptors>
</jaxws:server>
</beans>
服务接口如下:
@WebService
public interface IUser {
public void saveUser(User user);
public User getUser(int uid);
}
服务类如下:
public class UserImpl implements IUser {
private List<User> users=new ArrayList<User>();
public User getUser(int uid) {
for(User temp:users){
if(temp.getUid()==uid){
return temp;
}
}
return null;
}
public void saveUser(User user) {
// TODO Auto-generated method stub
users.add(user);
}
}
实体类如下:
public class User {
private int uid;
private String uname;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
}
通过JSP+Servlet调用本地服务:
Servlet在web.xml中配置如下:
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>loaderman.i.cxf.servlet.UserServlet</servlet-clas>
</servlet>
Servlet核心代码调用如下:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
User user = new User();
user.setUid(Integer.parseInt(request.getParameter("uid")));
user.setUname(request.getParameter("uname"));
userImpl.saveUser(user);
}
public void init() throws ServletException {
// Put your code here
WebApplicationContext springContext = WebApplicationContextUtils
.getWebApplicationContext(this.getServletContext());
userImpl = (IUser) springContext.getBean("userImpl");
}
WEB页面调用代码如下:
<form action="/demo/servlet/UserServlet" method="post">
用户编号:<input type="text" name="uid" /><br/>
用户名:<input type="text" name="uname" /><br/>
<input type="submit" value="提交" />
</form>
通过Java远程调用访问CXF+Spring服务如下:
public static void main(String[] args) {
IUserService userService=new IUserService();
User user=new User();
user.setUid(1);
user.setUname("admin");
userService.getIUserPort().saveUser(user);
User temp=userService.getIUserPort().getUser(1);
System.out.println(temp.getUid() + "|" + temp.getUname());
}
通过ajax远程调用访问CXF+Spring服务如下:
<body>
<button onclick="mobile()">cxf+Spring测试</button>
</body>
<script language="javascript">
// 1:创建XMLHTTP对象
var xhr=null;
function mobile(){
// 声明在访问的ws的地址
var url="http://localhost:8888/day01/ws/hello";
// 书写要发送的XML文件,即 SOAP
var soap='<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>' +
'<ns2:getUser xmlns:ns2="http://ws.spring.cxf.i.loaderman.cn/"><arg0>1</arg0></ns2:getUser></soap:Body></soap:Envelope>';
// 3:打开连接
xhr.open("POST",url,true);
xhr.setRequestHeader("Content-Type","text/xml; charset=utf-8");
xhr.setRequestHeader("Accept","*/*");
xhr.onreadystatechange=callBack;
xhr.send(soap);
} function callBack(){
if(xhr.readyState==4){
var a=xhr.responseXML;
alert(xhr.responseXML.getElementsByTagName("uid")[0].text);
alert(xhr.responseXML.getElementsByTagName("uname")[0].text);
}
} function init(){
xhr=new ActiveXObject("MSXML2.XMLHTTP.3.0");
} init();
</script>
CXF框架构建和开发 Services的更多相关文章
- webservice第三篇【接口开发webservice、CXF框架使用、IDEA下使用webservice、小例子】
实现接口的webservice 服务端 import javax.jws.WebService; /**面向接口的webservice发布方式 * * */ @WebService public in ...
- 使用CXF框架,发布webservice服务,并使用客户端远程访问webservice
使用CXF框架,发布webservice服务,并使用客户端远程访问webservice 1. CXF介绍 :soa的框架 * cxf 是 Celtrix (ESB框架)和 XFire(webs ...
- CXF框架介绍及Spring集成
1.CXF框架概念介绍 Apache CXF 是一个开源的 WebService 框架,CXF可以用来构建和开发 WebService,这些服务可以支持多种协议,比如:SOAP.POST/HTTP.H ...
- 转载 WebService 的CXF框架 WS方式Spring开发
WebService 的CXF框架 WS方式Spring开发 1.建项目,导包. 1 <project xmlns="http://maven.apache.org/POM/4.0 ...
- WebService系列二:使用JDK和CXF框架开发WebService
一.使用JDK开发WebService 服务端程序创建: 1.新建一个JDK开发webservice的服务端maven项目JDKWebServiceServer 2. 定义一个接口,使用@WebSer ...
- Eclipse+CXF框架开发Web服务实战
一. 说明 采用CXF框架开发webservice. 所用软件及版本如下. 操作系统:Window XP SP3. JDK:JDK1.6.0_07,http://www.oracle.com/ ...
- Cxf + Spring3.0 入门开发WebService
转自原文地址:http://sunny.blog.51cto.com/182601/625540/ 由于公司业务需求, 需要使用WebService技术对外提供服务,以前没有做过类似的项目,在网上搜寻 ...
- 【Java EE 学习 81】【CXF框架】【CXF整合Spring】
一.CXF简介 CXF是Apache公司下的项目,CXF=Celtix+Xfire:它支持soap1.1.soap1.2,而且能够和spring进行快速无缝整合. 另外jax-ws是Sun公司发布的一 ...
- 基于Dubbo框架构建分布式服务
Dubbo是Alibaba开源的分布式服务框架,我们可以非常容易地通过Dubbo来构建分布式服务,并根据自己实际业务应用场景来选择合适的集群容错模式,这个对于很多应用都是迫切希望的,只需要通过简单的配 ...
随机推荐
- PAT Basic 1088 三人行 (20 分)
子曰:“三人行,必有我师焉.择其善者而从之,其不善者而改之.” 本题给定甲.乙.丙三个人的能力值关系为:甲的能力值确定是 2 位正整数:把甲的能力值的 2 个数字调换位置就是乙的能力值:甲乙两人能力差 ...
- Python实现神经网络算法识别手写数字集
最近忙里偷闲学习了一点机器学习的知识,看到神经网络算法时我和阿Kun便想到要将它用Python代码实现.我们用了两种不同的方法来编写它.这里只放出我的代码. MNIST数据集基于美国国家标准与技术研究 ...
- Window脚本学习笔记之BAT调用设置
用一句bat脚本调用window的系统设置: rem 调用回收站 explorer.exe ::{645FF040-5081-101B-9F08-00AA002F954E} rem 检查Windows ...
- Paper Reading:推荐系统评价指标综述
论文:推荐系统评价指标综述 发表时间:2012 发表作者:朱郁筱,吕琳媛 论文链接:论文链接 本文对现有的推荐系统评价指标进行了系统的回顾,总结了推荐系统评价指标的最新研究进展,从准确度. 多样性.新 ...
- layer弹出框中icon数字参数说明
前言icon参数为0,如下代码: layer.msg(}); 运行结果如图: icon参数为1,如下图 icon参数为2,如下图: icon参数为3,如下图: icon参数为4,如下图: icon参数 ...
- linux下18种监测网络带宽方式
1. nload nload是一个命令行工具,让用户可以分开来监控入站流量和出站流量.它还可以绘制图表以显示入站流量和出站流量,视图比例可以调整.用起来很简单,不支持许多选项. 所以,如果你只需要快速 ...
- 关于MVC设计模式下的Model
内容1: 1.大多数情况下,会有两个关于Model的文件. 一个称他为Entity Model,他里面的字段一般是与数据库直接交互的,也就是说,Entity里面每一个字段赋予的属性都是对应着数据库来的 ...
- Reloading current route in Angular 5 / Angular 6 / Angular 7
问题: angular 从子状态回到当前的父级状态的时候,父级状态不会重新初始化. https://github.com/angular-ui/ui-router/issues/2992 原文:htt ...
- 15分钟入门Markdown
一.标题一 标题三 标题六 # 一.标题一 ### 标题三 ###### 标题六 二.字体 1.普通字体 字体加粗 斜体 斜体加粗 删除线 1.普通字体 **字体加粗** *斜体* ***斜体加粗** ...
- 015_linux驱动之_signal
1. 首先看应用程序 1. 首先分析第二点使用函数signal(SIGIO, my_signal_fun);来设置,当驱动程序传递信号给应用程序时候会调用第一点的程序 2. 第三点是设置相关参数 (二 ...