欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

使用Spring在web容器中部署apache-cxf的webservice(附完整源码),,webservice通常

来源: javaer 分享于  点击 2171 次 点评:190

使用Spring在web容器中部署apache-cxf的webservice(附完整源码),,webservice通常


webservice通常运行在web端,所以需要部署到web容器中,apache-cxf为我们做了很多事情,所以我们要做的就很少了。

首先需要新建一个web类型的maven项目,然后在pom中添加相应的依赖(完整的pom文件可以在源码中下载,这里不列出来了)。

然后打开web.xml编辑web.xml添加apache-cxf需要的servlet以及spring配置,如下web.xml内容:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app>  <display-name>Archetype Created Web Application</display-name>  <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>      <listener>          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>      </listener>      <context-param>          <param-name>contextConfigLocation</param-name>          <param-value>/WEB-INF/spring/context.xml</param-value>      </context-param>  </web-app>

在web.xml文件中我们定义了spring配置文件,需要在spring配置文件中配置apache-cxf的endpoint和client,如下context.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"      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd      http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">      <import resource="classpath:META-INF/cxf/cxf.xml" />      <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />      <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />      <jaxws:endpoint id="helloworld"         implementor="cn.outofmemory.hello.apache.cxf.web.HelloCxfImp" address="/HelloCxf" />      <!-- For client test -->      <jaxws:client id="helloworldClient" address="http://localhost:8080/hello-apache-cxf-web/ws/HelloCxf"     serviceClass="cn.outofmemory.hello.apache.cxf.web.HelloCxf" />     </beans>  

注意要在beans中添加jaxws相关的命名空间和schema。

正确做好这两个配置,并在web项目中实现webservice的接口定义和实现,就可以通过web容器访问webservice了。关于webservice的接口和实现定义请看这篇文章。

完整的源码下载

相关栏目:

用户点评