CXF+Spring整合发布SOAP服务(Web项目服务端)
**
一、原理
**
当需要调用一个Web项目的service或其他层的功能时,这时我们需要Web项目也能提供相关的服务,供其他项目使用。
**
二、开发步骤(方法一)
**
1、创建一个Web项目,假设创建了一个weatherService,提供天气查询的服务。
2、将该服务发布
第一步:
把【springmvc3.1.4与cxf2.7.11jar】jar包都导入,这里在IDEA工具导jar包的时候一定要注意,有时候会导不进去然后tomcat会报 classNotFound等错误,我们需要进行检查一下。
打开Project Structure窗口,选择Artifacts,选择要打包部署的项目,在Output Layout –> Web-INF查看是否有lib目录,如果右边Available Elements窗口还显示有jar包,说明这些jar包未添加,则应右击选择Put into Output Root,这样就OK啦~
第二步:
配置spring配置文件applicationContext.xml ,添加以下内容
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core
http://cxf.apache.org/schemas/core.xsd">
<!-- 通过spring发布webservice服务 -->
<!-- 1、创建 天气服务对象 -->
<bean id="weatherInterfaceImpl" class="com.ykd.service.WeatherInterfaceImpl"></bean>
<!-- 2、发布服务 -->
<jaxws:server address="/weather" serviceClass="com.ykd.service.WeatherInterface">
<jaxws:serviceBean>
<ref bean="weatherInterfaceImpl"/>
</jaxws:serviceBean>
</jaxws:server>
</beans>
第三步:配置web.xml加载spring,和CXF请求拦截,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<!--contextConfigLocation是不能修改的 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 1、监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 2.配置CXF servlet -->
<servlet>
<servlet-name>CXF</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<!-- 3.配置CXF servlet映射路径 -->
<servlet-mapping>
<servlet-name>CXF</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
</web-app>
第四步:布置到tomcat中,启动tomcat,在浏览器访问http://127.0.0.1:8080/ws/weather?wsdl 这时候能够看到wsdl说明书,说明发布成功。
**
二、开发步骤(方法一)
**
第一步:首先完成一个service服务
第二步:直接在applicationContext.xml中添加一下代码即可实现服务的发布。
<jaxws:endpoint address="/hello" implementor="com.ykd.service.HelloInterfaceImpl"> </jaxws:endpoint>
如图可以分别看到两个服务的wsdl说明书