CXF學習-與Spring整合(暴露web Service接口給其他程序使用)

基本步驟:

1、保證服務一啓動就加載Spring;

2、在web-xml中配置CXF的核心配置;

<?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"
     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"
     id="WebApp_ID" version="2.5">
  <display-name/>
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
  </context-param>
  
  <!-- 配置CXF -->
  <!-- 下面的配置說明凡是請求路徑爲/studyCXF/*均交給CXFServlet處理 -->
  <servlet>
      <servlet-name>CXFServlet</servlet-name>
      <!-- CXF核心配置:org.apache.cxf.transport.servlet.CXFServlet -->
      <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>CXFServlet</servlet-name>
      <url-pattern>/studyCXF/*</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


3、在Spring配置文件中導入CXF提供Schema、xml配置;

<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
	http://cxf.apache.org/jaxws
	http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 先寫命名空間,再寫具體路徑 -->
	
	<!-- web類加載路徑:兩類 webRoot下面的class以及lib路徑下面的jar文件 -->
	<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"/></beans>


4、在Spring配置文件中,使用jaxws:endpoint 來暴露webService;

5、如果要添加攔截器,在jaxws:endpoint裏面添加inInterceptors及outInterceptors元素。

 <!-- implementor提供者-->
    <!-- 設置爲以i個bean前面加#號 -->
	<jaxws:endpoint address="/fkjava" implementor="#helloWorldWS">
	    <!-- 添加攔截器 -->
	    <jaxws:inInterceptors>
	        <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
	        <bean class="cxf.ws.auth.AuthInInterceptor"></bean>
	    </jaxws:inInterceptors>
	    
	</jaxws:endpoint>

攔截器

package cxf.ws.auth;

import java.util.List;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
//通過AbstractPhaseInterceptor決定攔截器在哪裏起作用
public class AuthInInterceptor extends AbstractPhaseInterceptor<SoapMessage> {

	public AuthInInterceptor(){
		//顯示調用父類有參構造器
		super(Phase.PRE_INVOKE);
	}
	//實現自己的攔截器,需要實現handleMessage方法
	//handleMessage方法的形參SoapMessage就是攔截到的SOAP消息
	//一旦獲取了SOAP消息,就可以解析或修改SOAP消息
	@Override
	public void handleMessage(SoapMessage msg) throws Fault {
		System.out.println("-----------------------------");
		//得到消息的所有header
		List<Header> headers=msg.getHeaders();
		
		if(headers==null||headers.size()<1){
			throw new Fault(new IllegalArgumentException("Header爲空"));
		}
		Header firstHeader=headers.get(0);
		
		Element ele=(Element) firstHeader.getObject();
		
		NodeList userIds=ele.getElementsByTagName("userId");
		NodeList userPasss=ele.getElementsByTagName("userPass");
		
		if(userIds.getLength()<1){
			throw new Fault(new IllegalArgumentException("用戶名格式不對"));
		}
		if(userPasss.getLength()<1){
			throw new Fault(new IllegalArgumentException("密碼格式不對"));
		}
		String userId=userIds.item(0).getTextContent();
		
		String userPass=userPasss.item(0).getTextContent();
		
		if(!(userId.equals("Charles")&&userPass.equals("duyx5218"))){
			throw new Fault(new IllegalArgumentException("用戶名、密碼不匹配"));
		}
	}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章