CXF学习-与Spring整合(暴露web Service接口给其他程序使用)
来源:互联网 发布:小智零食淘宝店 编辑:程序博客网 时间:2024/06/11 10:38
基本步骤:
1、保证服务一启动就加载Spring;
2、在web-xml中配置CXF的核心配置;
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name/> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </context-param> <!-- 配置CXF --> <!-- 下面的配置说明凡是请求路径为/studyCXF/*均交给CXFServlet处理 --> <servlet> <servlet-name>CXFServlet</servlet-name> <!-- CXF核心配置:org.apache.cxf.transport.servlet.CXFServlet --> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/studyCXF/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
3、在Spring配置文件中导入CXF提供Schema、xml配置;
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:p="http://www.springframework.org/schema/p"xmlns:jaxws="http://cxf.apache.org/jaxws"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd"><!-- 先写命名空间,再写具体路径 --><!-- web类加载路径:两类 webRoot下面的class以及lib路径下面的jar文件 --><import resource="classpath:/META-INF/cxf/cxf.xml"/><import resource="classpath:/META-INF/cxf/cxf-servlet.xml"/><import resource="classpath:/META-INF/cxf/cxf-extension-soap.xml"/></beans>
4、在Spring配置文件中,使用jaxws:endpoint 来暴露webService;
5、如果要添加拦截器,在jaxws:endpoint里面添加inInterceptors及outInterceptors元素。
<!-- implementor提供者--> <!-- 设置为以i个bean前面加#号 --><jaxws:endpoint address="/fkjava" implementor="#helloWorldWS"> <!-- 添加拦截器 --> <jaxws:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> <bean class="cxf.ws.auth.AuthInInterceptor"></bean> </jaxws:inInterceptors> </jaxws:endpoint>
拦截器
package cxf.ws.auth;import java.util.List;import org.apache.cxf.binding.soap.SoapMessage;import org.apache.cxf.headers.Header;import org.apache.cxf.interceptor.Fault;import org.apache.cxf.phase.AbstractPhaseInterceptor;import org.apache.cxf.phase.Phase;import org.w3c.dom.Element;import org.w3c.dom.NodeList;//通过AbstractPhaseInterceptor决定拦截器在哪里起作用public class AuthInInterceptor extends AbstractPhaseInterceptor<SoapMessage> {public AuthInInterceptor(){//显示调用父类有参构造器super(Phase.PRE_INVOKE);}//实现自己的拦截器,需要实现handleMessage方法//handleMessage方法的形参SoapMessage就是拦截到的SOAP消息//一旦获取了SOAP消息,就可以解析或修改SOAP消息@Overridepublic void handleMessage(SoapMessage msg) throws Fault {System.out.println("-----------------------------");//得到消息的所有headerList<Header> headers=msg.getHeaders();if(headers==null||headers.size()<1){throw new Fault(new IllegalArgumentException("Header为空"));}Header firstHeader=headers.get(0);Element ele=(Element) firstHeader.getObject();NodeList userIds=ele.getElementsByTagName("userId");NodeList userPasss=ele.getElementsByTagName("userPass");if(userIds.getLength()<1){throw new Fault(new IllegalArgumentException("用户名格式不对"));}if(userPasss.getLength()<1){throw new Fault(new IllegalArgumentException("密码格式不对"));}String userId=userIds.item(0).getTextContent();String userPass=userPasss.item(0).getTextContent();if(!(userId.equals("Charles")&&userPass.equals("duyx5218"))){throw new Fault(new IllegalArgumentException("用户名、密码不匹配"));}}}
0 0
- CXF学习-与Spring整合(暴露web Service接口给其他程序使用)
- CXF-10: CXF 与 Spring 的整合 之 使用别人暴露的 Web Service 接口
- CXF学习-与Spring结合:使用其他程序提供的web service 接口
- CXF-09: CXF 与 Spring 的整合 之 暴露 Web Service 接口
- 简单WEB Service接口实现(CXF与spring整合)
- Web Service学习-CXF与Spring整合为JavaEE应用发布WebService(三)
- Web Service学习-CXF与Spring整合为JavaEE应用发布WebService(三)
- Web Service学习-CXF与Spring整合为JavaEE应用发布WebService(三)
- CXF-DOSGI学习笔记(一)-web service暴露
- CXF [web service]整合spring 总结
- Spring boot 整合CXF开发web service
- Spring boot 整合CXF开发web service
- Spring boot 整合CXF开发web service
- Spring boot 整合CXF开发web service
- Web Service (四) 手动发布Web Service接口-CXF与Spring集成
- Web Service (四) 手动发布Web Service接口-CXF与Spring集成(实战)
- 使用CXF和Spring搭建Web Service
- 使用Axis开发Web Service程序 整合 spring
- Codeforces Round #266 (Div. 2)
- Android参数分析
- 40_2 取出首尾相连的珠子中一段,要求包含所有N颜色,并长度最短。 滑动窗口问题
- 让非常规词带来网站流量
- java Comparator和Comparable应用
- CXF学习-与Spring整合(暴露web Service接口给其他程序使用)
- OpenCV获取和设置图像的平均亮度
- What is the Future for SharePoint?
- 40_3 设计一个系统处理词语搭配,比如中国和人民可以搭配
- [小技巧] 使用ssh登入不执行.bashrc解决方法
- iOS开发之网络开发——AFNetWorking
- ios设备分辨率 屏幕尺寸一览表
- 组合数学中 序数法生成一个新的序列
- 重载与覆盖区别