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

最近由于项目需要,在一个已经正常的系统中添加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确定是否正常工作。

 

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

猜你喜欢

转载自buxin-2008.iteye.com/blog/2054500