So easy Webservice 8.spring整合CXF 发布WS
1.添加jar包(cxf的jar包中包含了spring的jar包),添加spring配置文件
2.web.xml中配置CXFServlet,过滤WS服务的地址
<!-- 配置CXFServlet,实现地址过滤的功能,项目启动时实例化 -->
<servlet>
<servlet-name>cxfServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>csfServlet</servlet-name>
<!-- 访问 http://localhost:8080/工程名/ws/ 下的所有请求,都会交给当前servlet过滤 -->
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
<!-- 由于上面的url-pattern中配置了过滤路径,所有ws服务地址应配置为:http://localhost:8080/工程名/ws/xxxService -->
3.配置applicationContext.xml
3.1 web.xml中配置spring监听器,在项目启动时加载spring配置文件
<!-- 配置监听器:加载spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3.2 添加ws服务实现类(必须添加注解@WebService),在spring中配置服务实现类的bean
import java.util.ArrayList;
import java.util.List; import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService; @WebService(
serviceName="UserService",
name="user",
portName="UserServicePort",
targetNamespace="com.mlxs.ws.cxfws.spring"
)
public class UserServiceImpl { private static List<User> ulist = new ArrayList<User>(); @WebMethod(operationName="add")
public void add(@WebParam(name="u") User u) {
ulist.add(u);
} @WebMethod(operationName="query")
//@WebResult 如果是集合,指定的是集合中存放的对象名称
public @WebResult(name="user") List<User> query() {
return ulist;
} }
<bean id="userService" class="com.mlxs.ws.cxfws.spring.UserServiceImpl"/>
3.3 添加jaxws标签
在cxf.jar中,找到/schemas/jaxws.xsd,再查找命名空间:targetNamespace="http://cxf.apache.org/jaxws",将内容复制到spring文件中:
xmlns:jaxws="http://cxf.apache.org/jaxws"
再在xsi:schemaLocation中添加(别名 实际地址):
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
3.4 配置CXF, 把服务实现类bean配置发布成WS服务 ;
address="/user"中不用配置前缀,ws地址在web.xml中servlet的配置的:http://localhost:8080/工程名/ws/user;
配置日志;
<jaxws:server address="/user">
<jaxws:serviceBean>
<ref bean="userService"/>
</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>
4.部署到tomcat,启动服务,使用浏览器访问http://localhost:8080/webservice_cxf_server/ws/user?WSDL
<wsdl:definitions xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="com.mlxs.ws.cxfws.spring" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="UserService" targetNamespace="com.mlxs.ws.cxfws.spring">
<wsdl:types>
<xs:schema xmlns:tns="com.mlxs.ws.cxfws.spring" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" targetNamespace="com.mlxs.ws.cxfws.spring" version="1.0">
<xs:element name="add" type="tns:add"/>
<xs:element name="addResponse" type="tns:addResponse"/>
<xs:element name="query" type="tns:query"/>
<xs:element name="queryResponse" type="tns:queryResponse"/>
<xs:complexType name="query">
<xs:sequence/>
</xs:complexType>
<xs:complexType name="queryResponse">
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="user" type="tns:user"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="user">
<xs:sequence>
<xs:element name="id" type="xs:int"/>
<xs:element minOccurs="0" name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="add">
<xs:sequence>
<xs:element minOccurs="0" name="u" type="tns:user"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="addResponse">
<xs:sequence/>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="queryResponse">
<wsdl:part element="tns:queryResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="add">
<wsdl:part element="tns:add" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="addResponse">
<wsdl:part element="tns:addResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="query">
<wsdl:part element="tns:query" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="user">
<wsdl:operation name="query">
<wsdl:input message="tns:query" name="query"></wsdl:input>
<wsdl:output message="tns:queryResponse" name="queryResponse"></wsdl:output>
</wsdl:operation>
<wsdl:operation name="add">
<wsdl:input message="tns:add" name="add"></wsdl:input>
<wsdl:output message="tns:addResponse" name="addResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="UserServiceSoapBinding" type="tns:user">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="query">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="query">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="queryResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="add">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="add">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="addResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="UserService">
<wsdl:port binding="tns:UserServiceSoapBinding" name="UserServicePort">
<soap:address location="http://localhost:8080/webservice_cxf_server/ws/user"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
5.使用myeclipse SOAP浏览器浏览:
调用add服务接口添加数据:
查看控制台:
Address: http://localhost:8080/webservice_cxf_server/ws/user
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=utf-8
Headers: {Accept=[application/soap+xml, application/dime, multipart/related, text/*], cache-control=[no-cache], connection=[close], Content-Length=[371], content-type=[text/xml; charset=utf-8], host=[localhost:8080], pragma=[no-cache], SOAPAction=[""], user-agent=[IBM Web Services Explorer]}
Payload: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="com.mlxs.ws.cxfws.spring" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<q0:add>
<u>
<id>1</id>
<name>admin</name>
</u>
</q0:add>
</soapenv:Body>
</soapenv:Envelope>
调用查询服务接口query:
控制台:
----------------------------
ID: 12
Address: http://localhost:8080/webservice_cxf_server/ws/user
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=utf-8
Headers: {Accept=[application/soap+xml, application/dime, multipart/related, text/*], cache-control=[no-cache], connection=[close], Content-Length=[288], content-type=[text/xml; charset=utf-8], host=[localhost:8080], pragma=[no-cache], SOAPAction=[""], user-agent=[IBM Web Services Explorer]}
Payload: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="com.mlxs.ws.cxfws.spring" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<q0:query/>
</soapenv:Body>
</soapenv:Envelope> --------------------------------------
2016-1-28 23:38:35 org.apache.cxf.interceptor.AbstractLoggingInterceptor log
信息: Outbound Message
---------------------------
ID: 12
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:queryResponse xmlns:ns2="com.mlxs.ws.cxfws.spring"><user><id>1</id><name>admin</name></user><user><id>2</id><name>魅力小生</name></user></ns2:queryResponse></soap:Body></soap:Envelope>
--------------------------------------
也可以自己写jsp或者java测试类进行ws服务接口的访问。。。
So easy Webservice 8.spring整合CXF 发布WS的更多相关文章
- Spring整合CXF发布及调用WebService
这几天终于把webService搞定,下面给大家分享一下发布webService和调用webService的方法 添加jar包 (官方下载地址:http://cxf.apache.org/downlo ...
- Spring整合CXF之发布WebService服务
今天我们来讲下如何用Spring来整合CXF,来发布WebService服务: 给下官方文档地址:http://cxf.apache.org/docs/writing-a-service-with-s ...
- Spring整合CXF,发布RSETful 风格WebService(转)
Spring整合CXF,发布RSETful 风格WebService 这篇文章是承接之前CXF整合Spring的这个项目示例的延伸,所以有很大一部分都是一样的.关于发布CXF WebServer和Sp ...
- Spring整合CXF,发布RSETful 风格WebService
原文地址:http://www.cnblogs.com/hoojo/archive/2012/07/23/2605219.html 这篇文章是承接之前CXF整合Spring的这个项目示例的延伸,所以有 ...
- SpringMVC4整合CXF发布WebService
SpringMVC4整合CXF发布WebService版本:SpringMVC 4.1.6,CXF 3.1.0项目管理:apache-maven-3.3.3 pom.xml <project x ...
- Spring整合CXF步骤,Spring实现webService,spring整合WebService
Spring整合CXF步骤 Spring实现webService, spring整合WebService >>>>>>>>>>>> ...
- Java WebService 教程系列之 Spring 整合 CXF
Java WebService 教程系列之 Spring 整合 CXF 一.引入 jar 包 <dependency> <groupId>org.apache.cxf</ ...
- Spring集成CXF发布WebService并在客户端调用
Spring集成CXF发布WebService 1.导入jar包 因为官方下载的包里面有其他版本的sprring包,全导入会产生版本冲突,所以去掉spring的部分,然后在项目根目录下新建了一个CXF ...
- Spring整合CXF webservice restful 实例
webservice restful接口跟soap协议的接口实现大同小异,只是在提供服务的类/接口的注解上存在差异,具体看下面的代码,然后自己对比下就可以了. 用到的基础类 User.java @Xm ...
随机推荐
- svn使用相关问题:eclipse插件,加锁,解锁,偷锁,更新不了,记住密码
svn使用相关问题:eclipse插件,加锁,解锁,偷锁,更新不了,记住密码 获取锁的时候可以看下 是谁锁住了,让对方提交解锁,如果是给离职人员锁住需要使用偷锁的方式先解锁再提交偷锁处理办法:选中该文 ...
- TI BLE CC2541的通讯协议.
包类型: 01命令/02数据/03应答消息 开始标志FF/本数据包长度(注意是16进制)/校验码/包ID/包类型01: 表示是命令/01表示下面要开始传输/03字符串编号/字符串长度/结束位FEFF ...
- PHP的Socket通信之UDP篇
1.创建一简单的UDP服务器 //服务器信息 $server = 'udp://127.0.0.1:9998'; //消息结束符号 $msg_eof = "\n"; $socket ...
- java面试每日一题11
题目:求1+2!+3!+...+20!的和 public class Recursion { public static void main(String args[]) throws NumberF ...
- java 面试每日一题
题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半:再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高? import java.util.Scanner; public cl ...
- 系统图片uri的问题
调用系统图库会出现两种uri的问题,一个是在文件管理器中的图库中,获取到的地址为:content://media/external/images/media/972 这种格式 另外一种的是系统文件管 ...
- [HTML]js实现页面跳转,页面A跳到另一个页面B.以及页面传值(中文)
要实现从一个页面A跳到另一个页面B,js实现就在A的js代码加跳转代码 JS跳转大概有以下几种方式: 第一种:(跳转到b.html)<script language="javascri ...
- ACM题目————士兵杀敌(三)
[RMQ算法]:用于当数组过于庞大的时候,查询区间的最大(最小)值. 时间复杂度:O(nlogn),主要时间发费在预处理上,查询只要O(1). 描述 南将军统率着N个士兵,士兵分别编号为1~N,南将军 ...
- 【转】西门子数控系统中MMC、PCU、NCU、CCU简略介绍
转载地址:http://cyj221.blog.163.com/blog/static/34194117201093005526170/ 2010-10-30 01:06:09| 分类: 机械制造 ...
- 3G中的A-GPS移动定位技术
位置业务(LBS,Location Based Service)是指移动网络通过特定的定位技术来获取移动终端的位置信息,从而为终端用户提供附加服务的一种增值业务,可广泛应用于紧急救援.导航追踪.运输调 ...