CXF學習-與Spring結合:使用其他程序提供的web service 接口

package crazyit.call.cxf;

import java.util.HashMap;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;

import cxf.ws.Cat;
import cxf.ws.Entry;
import cxf.ws.HelloWorld;
import cxf.ws.StringCat;

public class ListCatAction extends ActionSupport {
	
	private HelloWorld hw;

	public HelloWorld getHw() {
		return hw;
	}

	public void setHw(HelloWorld hw) {
		this.hw = hw;
	}
	
	private Map<String,Cat> cats=new HashMap<String, Cat>();

	public Map<String, Cat> getCats() {
		return cats;
	}

	public void setCats(Map<String, Cat> cats) {
		this.cats = cats;
	}
	
	public String execute(){
		StringCat sc=hw.getAllCats();
		for(Entry entry:sc.getEntries()){
			cats.put(entry.getKey(), entry.getValue());
		}
		setCats(cats);
		return SUCCESS;
	}
	
}

步驟:

1、讓Action依賴遠程web service接口;

1)添加一個action 在action裏面調用webService代理

2)在spring配置文件裏面

2、複製CXF核心JAR包;
3、在Spring配置文件中導入CXF提供Schema、xml配置;
4、在Spring配置文件裏面配置 jaxws:client, 配置遠程Web Service代理;
5、如果要添加攔截器,在jaxws:client裏面添加inInterceptors及outInterceptors元素。

<?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: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"/>
	
	<!-- action類依賴遠程webService 爲了保證web Service 的代理自動分配給action,client 的id需要與setter方法名字匹配 -->
	<jaxws:client id="hw"
	    address="http://10.0.6.17/fightUp"
	    serviceClass="cxf.ws.HelloWorld">
	    <jaxws:outInterceptors>
	        <bean class="crazyit.call.cxf.auth.AuthOutInterceptor">
	            <constructor-arg value="Charles"/>
	            <constructor-arg value="duyx5218"/>
	        </bean>
	    </jaxws:outInterceptors>
	</jaxws:client>
</beans>

攔截器

package crazyit.call.cxf.auth;

import java.util.List;

import javax.xml.namespace.QName;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class AuthOutInterceptor extends AbstractPhaseInterceptor<SoapMessage> {

	private String userId;
	
	private String userPass;
	
	public AuthOutInterceptor(String userId,String userPass){
		super(Phase.PREPARE_SEND);//在準備發送SOAP消息時,啓用該攔截器
		this.userId=userId;
		this.userPass=userPass;
	}

	@Override
	public void handleMessage(SoapMessage msg) throws Fault {
		List<Header> headers=msg.getHeaders();
		//創建document對象
		Document doc=DOMUtils.createDocument();
		
		Element ele=doc.createElement("authHeader");
		//此處創建的元素,應該按照服務器那邊的要求
		Element idEle=doc.createElement("userId");
		Element passEle=doc.createElement("userPass");
		
		idEle.setTextContent(userId);
		passEle.setTextContent(userPass);
		
		ele.appendChild(idEle);
		ele.appendChild(passEle);
		//把ele包裝成header
		headers.add(new Header(new QName("fkjava"), ele));
	}
}


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