CXF学习总结3 客户端编写

1.生成客户端代码

 

cmd下进入项目Src目录下:
cd D:\Workspaces\MyEclipse 10\WebService_CXF_Client\src
D:
生成客户端代码 wsdl2java wdsl地址
wsdl2java http://localhost:8080/WebService_CXF/WebService/userManager/UserServiceWs?wsdl

2.编写客户端拦截器(添加用户名告诉服务端自己是合法调用者)

 

package interceptor;

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.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class AuthInterceptor extends AbstractPhaseInterceptor<SoapMessage> {

	Logger logger=Logger.getLogger(AuthInterceptor.class);
	
	private String userName;
	private String passWord;

	public AuthInterceptor(String userName, String passWord) {
//在发送消息之前调用
		super(Phase.PREPARE_SEND);
		this.userName=userName;
		this.passWord=passWord;
	}

	@Override
	public void handleMessage(SoapMessage msg) throws Fault {
		
		List<Header>  headers=msg.getHeaders();
//		创建Document
		Document doc=DOMUtils.createDocument();

//		此处的Element名字根据服务端指定
		Element eleName=doc.createElement("userName");
		eleName.setTextContent(this.userName);
		Element elePass=doc.createElement("passWord");
		elePass.setTextContent(this.passWord);
		
		Element eleHead=doc.createElement("authHead");
		eleHead.appendChild(eleName);
		eleHead.appendChild(elePass);
		
		/*
		 * 	<authHead>
				<userName>rain</userName>
				<passWord>rain</passWord>
			</authHead>
		 * 
		 * */
		
//		把ele包装成Head并添加到Soap消息的Header列表中
		headers.add(new Header(new QName("rain"), eleHead));
	}

}


 

3.Spring配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:jaxws="http://cxf.apache.org/jaxws" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.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,发布的地址,实现的接口,拦截器1,拦截器2-->
	
	<jaxws:client id="userWsClient" 
			address="http://localhost:8080/WebService_CXF/WebService/userManager/UserServiceWs" 
			serviceClass="com.cxf.ws.IUserServiceWs">
			<jaxws:outInterceptors>
				<bean class="interceptor.AuthInterceptor">
					<constructor-arg value="rain"/>
					<constructor-arg value="rain"/>
				</bean>			
				<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>			
			</jaxws:outInterceptors>
	</jaxws:client>
	
</beans>


 

4测试

package Client;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cxf.ws.IUserServiceWs;
import com.cxf.ws.UserMan;

public class TestClient {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {}

	@Test
	public void test() {
		ApplicationContext app=new ClassPathXmlApplicationContext("spring-test.xml");
		IUserServiceWs service= app.getBean("userWsClient", IUserServiceWs.class);
		UserMan man=service.getUserByName("ddd");
		System.out.println(man.getName()+man.getPassword());
	}

//发出的Soap消息格式      
//  http://localhost:8080/WebService_CXF/WebService/userManager/UserServiceWs?wsdl   
//  <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">   
//  <soap:Header>   
//      <authHead>   
//          <userName>rain1</userName>   
//          <passWord>rain</passWord>   
//      </authHead>   
//  </soap:Header>   
//  <soap:Body>   
//      <ns2:getUserByName xmlns:ns2="http://ws.cxf.com/">   
//          <UserName>AA</UserName>   
//      </ns2:getUserByName>   
//  </soap:Body>   
//</soap:Envelope>
}



 


 

 

你可能感兴趣的:(CXF学习总结3 客户端编写)