1. 程式人生 > >Spring+CXF3.1.3搭建SOAP服務端(一)

Spring+CXF3.1.3搭建SOAP服務端(一)

1.      版本說明
 從CXF官網下載得到CXF3.1.3,該版本需要JDK7.0的支援,並且最高可以支援到Spring4.1.x版本

2.      Spring4.1.8+CXF3.1.3 所需要的全部jar包列表


3.     專案結構


4.     搭建過程詳述

a) web.xml (配置spring監聽和cxf配置等)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd ">

	  <!-- 載入Spring容器監聽 -->
	  <listener>    
	        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	  </listener>
	  
	  <!-- 設定Spring容器載入配置檔案路徑 -->
	  <context-param>    
	      <param-name>contextConfigLocation</param-name>    
	      <param-value>WEB-INF/application/applicationContext-*.xml</param-value>
	  </context-param>
	  
	  <!--配置Springmvc核心控制器-->
	  <servlet>          
	        <servlet-name>springmvc</servlet-name>         
	        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	        <load-on-startup>1</load-on-startup>
	  </servlet>    
	  <!--為DispatcherServlet建立對映 -->      
	  <servlet-mapping>  
	        <servlet-name>springmvc</servlet-name>      
	        <url-pattern>*.do</url-pattern>    
	  </servlet-mapping> 
	 
	 <!-- CXF的配置 --> 
	 <servlet>    
        <servlet-name>CXFServlet</servlet-name>    
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>    
        <load-on-startup>2</load-on-startup>    
   	 </servlet> 
    <servlet-mapping>    
        <servlet-name>CXFServlet</servlet-name>    
        <url-pattern>/webservice/*</url-pattern>    
    </servlet-mapping>   
</web-app>

b) springmvc-servlet.xml (配置自動掃描註解、事務處理等)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
	 xmlns:context="http://www.springframework.org/schema/context"
	 xmlns:mvc="http://www.springframework.org/schema/mvc"  
	 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	 xmlns:tx="http://www.springframework.org/schema/tx" 
	 xsi:schemaLocation="
	 	  http://www.springframework.org/schema/beans     
          http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-4.1.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
          http://www.springframework.org/schema/tx     
          http://www.springframework.org/schema/tx/spring-tx-4.1.xsd   
          http://www.springframework.org/schema/context     
          http://www.springframework.org/schema/beans/spring-beans-4.1.xsd ">
	 
	 <!-- 啟動註解驅動-->  
	 <mvc:annotation-driven/>  
     <!-- 啟動包掃描功能-->  
     <context:component-scan base-package="com.cxf.*" />  
     
</beans>  

c) IBaseWebService (服務端介面程式碼)
package com.cxf.service;

import javax.jws.WebService;

@WebService
public interface IBaseWebService {
	
	public String HelloWorld();

}

d)BaseWebServiceImp(服務端介面實現類程式碼)

package com.cxf.service.imp;

import javax.jws.WebService;

import com.cxf.service.IBaseWebService;

@WebService(endpointInterface="com.cxf.service.IBaseWebService",serviceName="Hello")
public class BaseWebServiceImp implements IBaseWebService {
	
	public String HelloWorld() {
		String message = "你好! Spring Mvc + cxf 第一個測試案例!";
		return message;
	}

}

e)applicationContext-cxf.xml( cxf服務端釋出配置)

<?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-4.1.xsd  
                            http://cxf.apache.org/jaxws   
                            http://cxf.apache.org/schemas/jaxws.xsd">
 
 <!-- cxf3.0及以上版本不需要再手工引入
    <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" />  --> 
     
    <!-- webservice服務l-->
    <jaxws:endpoint id="HelloWorld" implementor="com.cxf.service.imp.BaseWebServiceImp" address="/SayHello" /> 
 
</beans>

5.     檢視專案中建立的所有為service(http://localhost:8080/cxf/webservice)

6.     檢視Hello服務的wsdl(該wsdl可以為客服端提供,生成程式碼提供幫助)