基于Spring和CXF的webservice开发环境搭建

来源:互联网 发布:最好的网络直播 编辑:程序博客网 时间:2024/06/06 07:18

使用CXF发布webservice服务时,规范的做法是先书写一个接口,用以声明服务类型。

基于Spring和CXF开发web service的框架搭建

一、创建web项目

Eclipse中新建一个dynamic webproject,命名为:CXFTest

二、导入需要的jar包

把下载的CXF项目的解压缩文件中lib文件夹下的所有jar包拷贝到WebContent->WEB-INF->lib文件夹下

三、创建服务接口

在Java resource->src目录下新建package包:com.cxfspring.service,并在该包中新建服务接口Ipeople,Ipeople接口代码如下:

package com.cxfspring.service; import javax.jws.WebMethod;import javax.jws.WebService; @WebServicepublic interface IPeople{    @WebMethod    public String sayHello(String name);}


四、创建服务实现类

在Java resource->src目录下新建package包:com.cxfspring.serviceImpl,并在该包中新建服务实现类Student,Student实现Ipeople接口,Student类代码如下:

package com.cxfspring.serviceImpl; import javax.jws.WebMethod;import javax.jws.WebService; import com.cxfspring.service.IPeople;  @WebServicepublic class Student implements IPeople{          @WebMethod         @Override         publicString sayHello(String name)         {                   //TODO Auto-generated method stub                   System.out.println("Hello:"+name);                  return"Hello:"+name+"nice to meet you!";         } }


五、配置web.xml文件

在WebContent->WEB-INF目录下新建web.xml,配置代码如下:

<?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"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0">  <display-name>ServiceManagement</display-name>   <!-- 加载Spring容器 -->   <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>   <!-- 配置CXF的核心servlet -->  <context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/applicationContext.xml</param-value></context-param>  <servlet>    <servlet-name>cxf</servlet-name>    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>    <!-- <init-param>        配置Spring的配置文件        <param-name>config-location</param-name>        <param-value>/WEB-INF/applicationContext.xml</param-value>    </init-param>-->    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>cxf</servlet-name>    <url-pattern>/*</url-pattern>  </servlet-mapping>   <welcome-file-list>     <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>


六、配置Spring配置文件

在WebContent->WEB-INF目录下新建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:tx="http://www.springframework.org/schema/tx"xmlns:jaxws="http://cxf.apache.org/jaxws"xmlns:jaxrs="http://cxf.apache.org/jaxrs"xmlns:jms="http://cxf.apache.org/transport/jms"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/tx/spring-aop.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/context/spring-context.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsdhttp://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsdhttp://cxf.apache.org/transport/jmshttp://cxf.apache.org/schemas/configuration/jms"> <!-- 导入CXF为扩展Spring提供的几个XML配置文件 --><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"/><import resource="classpath:META-INF/cxf/cxf-extension-object-binding.xml"/> <!-- 利用endpoint发布服务 --><jaxws:endpoint id="HelloService"implementor="com.cxfspring.serviceImpl.Student" address="/HelloService"/> </beans>


七、验证服务是否能发布

把工程部署到tomcat上,并启动tomcat,在浏览器中输入http://localhost:8090/CXFTest/HelloService?wsdl 。如果浏览器能显示该服务的WSDL文档,则说明该服务能成功发布。


最后把本文搭建的框架共享出来,http://download.csdn.net/detail/longshengguoji/8270873

2 0
原创粉丝点击