1. 程式人生 > >使用CXF 開發SOAP 的webService介面客戶端

使用CXF 開發SOAP 的webService介面客戶端

上次我們講到了 soap的webService的服務端,接下來我們將會繼續寫改例項的客戶端,

這個例項是往服務端同步資料的介面服務,所以要和服務端的例子一樣準備wsdl 和xsd ,以及在maven工程中引入cxf 生成wsdl 的外掛。 然後生成對應的 request、

response、header等實體,以及生成介面的服務Service。

然後客戶端要建立pojo實體,用來進行封裝推送的資訊。

UserEntity類:

/**
 * 使用者實體
 * @author leo
 *
 */
public class UserEntity {
	//賬號
	private String userCode;
	//姓名
	private String userName;
	//密碼
	private String password;
	public String getUserCode() {
		return userCode;
	}
	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
}
接下來建立客戶端的介面,以及實現類,
/**
 * 模擬webService客戶端
 * @author leo
 *
 */
public interface SyncUserClient {
	void syncUser(UserEntity userEntity);
}
實現類SyncUserClientImpl.java的程式碼

package com.deppon.soap.client.Service.impl;

import java.util.UUID;

import javax.xml.ws.Holder;

import com.deppon.soap.bean.UserEntity;
import com.deppon.soap.client.Service.SyncUserClient;
import com.deppon.soap.header.ESBHeader;
import com.deppon.soap.soapdemoservice.CommonException;
import com.deppon.soap.soapdemoservice.DemoRequest;
import com.deppon.soap.soapdemoservice.SoapDemoService;
/**
 * <span style="font-family: Arial, Helvetica, sans-serif;">模擬客戶端的實現</span>
 * @author leo
 *
 */
public class SyncUserClientImpl implements SyncUserClient{
	//
	private SoapDemoService soapDemoService;
	
	public void setSoapDemoService(SoapDemoService soapDemoService) {
		this.soapDemoService = soapDemoService;
	}


	@Override
	public void syncUser(UserEntity userEntity) {
		ESBHeader header = new ESBHeader();
		// 設定服務編碼
		header.setEsbServiceCode("test");
		header.setMessageFormat("SOAP");
		header.setSourceSystem("soap test");
		header.setExchangePattern(1);
		header.setVersion("1.0");
		header.setRequestId(UUID.randomUUID().toString());
		Holder<ESBHeader> holder = new Holder<ESBHeader>(header);

		DemoRequest request = new DemoRequest();
		request.setUserCode(userEntity.getUserCode());
		request.setUserName(userEntity.getUserName());
		request.setTitle("haha");
		
		try {
			//呼叫介面
			soapDemoService.soapDemoService(holder, request);
		} catch (CommonException e) {
			e.printStackTrace();
		}
	}
	
}
最後,由於Cxf 要依賴於spring的整合,且是webApp 服務,所以要在web.xml中配置CXF、spring等監聽和過濾驗證。然後在ds-spring.xml 中注入客戶端的依賴,以及cxf主持spring整合的客戶端配置:
<bean id="syncUserClientImpl" class="com.deppon.soap.client.Service.impl.SyncUserClientImpl">
		<property name="soapDemoService" ref="soapDemoService"></property>
	</bean>
	<!--  client -->
	<jaxws:client id="soapDemoService" serviceClass="com.deppon.soap.soapdemoservice.SoapDemoService" 
	address="http://127.0.0.1:8080/test/soap/userServiceImpl">
		<jaxws:inInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
		</jaxws:inInterceptors>
		<jaxws:outInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
		</jaxws:outInterceptors>
	</jaxws:client>
都配置好了之後,先啟動服務端,然後寫一個junit 測試類進行測試客戶端,
/**
 * 客戶端測試類
 * @author leo
 *
 */
public class TestClient {
	private static final Log LOG = LogFactory.getLog(TestClient.class);
	
	private SyncUserClient syncUserClientImpl;
	
	
	public void setSyncUserClientImpl(SyncUserClient syncUserClientImpl) {
		this.syncUserClientImpl = syncUserClientImpl;
	}
	@Before
	public void setup(){
		syncUserClientImpl = (SyncUserClient) SpringTestHelper.get().getBeanByClass(SyncUserClientImpl.class);
	}
	/*
	 *測試客戶端 
	 */
	@Test
	public void test() {
		UserEntity entity =createUser();
		syncUserClientImpl.syncUser(entity);
		
	}
	/**
	 * 建立實體物件
	 * @return
	 */
	public  UserEntity createUser(){
		UserEntity  entity = new UserEntity();
		entity.setUserName("老闆");
		entity.setUserCode("000001");
		entity.setPassword("000000");
		return entity;
	}
}


看到服務端已經打印出 插入的資料,說明介面同步成功! 好了一個完整的客戶端服務已經開發完畢。