当前位置:首页 > 开发 > 编程语言 > Web Service > 正文

Spring framework、Spring MVC、Apache CXF搭建webService环境

发表于: 2014-04-25   作者:buxin_2008   来源:转载   浏览次数:
摘要: 最近由于项目需要,在一个已经正常的系统中添加webservice,原系统由spring framework(3.2.0.RELEASE)、spring mvc(3.2.0.RELEASE)、spring security(3.1.0.RELEASE)搭建,maven(3.1)管理包依赖。 1.首先编辑原pom.xml,添加apache cxf(2.7.10)包: <!-- Ap

最近由于项目需要,在一个已经正常的系统中添加webservice,原系统由spring framework(3.2.0.RELEASE)、spring mvc(3.2.0.RELEASE)、spring security(3.1.0.RELEASE)搭建,maven(3.1)管理包依赖。

1.首先编辑原pom.xml,添加apache cxf(2.7.10)包:

<!-- Apache CXF (start)-->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency> 
  	<dependency>
    	    <groupId>org.apache.cxf</groupId>
   	    <artifactId>cxf-rt-core</artifactId>
    	    <version>${cxf.version}</version>
    	    <type>jar</type>
    	    <scope>compile</scope>
  	</dependency>
 <!-- Apache CXF (end)-->

 2.在src目录添加目录com.ltpc.hwws.wservice包,包中创建接口HelloWorld,如下:

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface HelloWorld {
	public String sayHello(@WebParam(name="name") String name);
}

 3.在wservice包下创建impl包,impl包中添加接口HelloWorld的实现类HelloWorldImpl,如下:

import com.itrus.ukey.wservice.HelloWorld;

public class HelloWorldImpl implements HelloWorld {

	@Override
	public String sayHello(String name) {
		System.out.println(name+"say hello");
		return "Hello "+name+",This is world.";
	}

}

 4.在src下添加webservice配置文件applicationContext-ws.xml,文件内容如下:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.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"/>
	<bean id="helloService" class="com.ltpc.hwws.wservice.impl.HelloWorldImpl" />
	<jaxws:endpoint id="helloWService" implementorClass="com.ltpc.hwws.wservice.HelloWorld" address="/hellows">
		<jaxws:implementor>
			<ref bean="helloService"/>
		</jaxws:implementor>
	</jaxws:endpoint>
</beans>

 5.修改配置web.xml文件,添加cxf servlet,如下:

<servlet>
	<servlet-name>CXFServlet</servlet-name>
	<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
	<servlet-name>CXFServlet</servlet-name>
	<url-pattern>/wservices/*</url-pattern>
</servlet-mapping>

 同时要添加对applicationContext-ws.xml文件的加载,全部配置完之后的web.xml文件,如下:

<?xml version="1.0" encoding="ISO-8859-1"?>
<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" version="2.5">
  <display-name>ltpc</display-name>
  <description>Roo generated ltpc application</description>
  <context-param>
    <param-name>defaultHtmlEscape</param-name>
    <param-value>true</param-value>
  </context-param>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext*.xml</param-value>
  </context-param>
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter>
    <filter-name>HttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
    <filter-name>HttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
	<servlet-name>CXFServlet</servlet-name>
	<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  </servlet>
  <servlet-mapping>
	<servlet-name>CXFServlet</servlet-name>
	<url-pattern>/wservices/*</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>ukey</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:config/webmvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>ukey</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

 6.配置完成,启动tomcat,在浏览器中访问http://127.0.0.1:8888/ltpc/wservices/hellows?wsdl确定是否正常工作。

 

 至此工作完成,用于备忘。

Spring framework、Spring MVC、Apache CXF搭建webService环境

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
1.创建类 2.接口编写 1 package com.fan; 2 3 import javax.jws.WebService; 4 5 @WebService 6 pub
1、Spring Mvc环境的搭建 到Spring的官网下载相应的jar包,笔者使用的是spring3.1.1的版本。 2、新
如何在Spring 3 MVC框架下结合CXF开发Webservice服务 1:在Web.xml中配置WebService URL过滤器。 &l
=========Spring MVC============================================================== web.xml <
=========Spring MVC============================================================== web.xml <
=========Spring MVC============================================================== web.xml <
最近手上一个项目要开发webservice,而原有系统使用了spring,所以在选择框架的时候,我选择了cxf,
1:版本3.1.2 2:建立web 工程,导入的jar包括 可能有些包是不需要的: 先看web.xml的配置文件,主
1.下载cfx http://cxf.apache.org/download.html 在项目中引入jar 2.创建服务接口类 import javax.j
首先,CXF和spring整合需要准备如下jar包文件: 这边我是用Spring的jar包是Spring官方提供的,并没
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号