採用CXF框架發佈webservice

package cn.it.ws.cxf.a;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService(serviceName="languageManager")
public interface LanduageService {

	@WebResult(name="language") String getLanguage(@WebParam(name="position") int position);
	

}
package cn.it.ws.cxf.a;

import org.apache.cxf.frontend.ServerFactoryBean;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

/**
 * 開發語言排行描述服務
 * @author wangzhiwei
 *
 */
public class LanduageServiceImpl implements LanduageService {
	/* (non-Javadoc)
	 * @see cn.it.ws.cxf.a.LanduageService#getLanguage(int)
	 */
	@Override
	public String getLanguage(int position) {
		String language=null;
		switch (position) {
		case 1:
			language="java";
			break;
		case 2:
			language="C";
			break;
		case 3:
			language="Objective-C";
			break;
		case 4:
			language="C#";
			break;			
		default:
			break;
		}
		return language;
	}
	/**
	 * 經過cxf框架發佈webservice
	 * 1.ServerFactoryBean
	 * 	- 不設置註解也能夠發佈webservice服務,不支持註解
	 *  -不支持攔截器的添加
	 * 2.JaxWsServerFactoryBean
	 *  -支持註解
	 *  -能夠添加攔截器
	 *  3.webservice訪問步驟:
	 *  1.檢測本地代理描述的wsdl是否與服務端的wsdl一致,俗稱爲握手
	 *  2.經過soap協議實現通訊,採用的是post請求,數據封裝在知足soap規約的xml中
	 *  3.返回數據一樣採用的是soap通訊,數據封裝在知足soap規約的xml中
	 */
/*	public static void main(String[] args) {
		LanduageService landuageService = new LanduageServiceImpl();
		ServerFactoryBean bean = new ServerFactoryBean();
		//Endpoint:地址
		bean.setAddress("http://127.0.0.1:9999/ws/cxf/languageService");
		bean.setServiceClass(LanduageService.class);//對外提供服務的業務類或接口
		bean.setServiceBean(landuageService);//服務的實現bean
		bean.create();//建立,發佈webservice
		System.out.println("wsdl地址:http://127.0.0.1:9999/ws/cxf/languageService?WSDL");
	}*/
	
	public static void main(String[] args) {
		LanduageService landuageService = new LanduageServiceImpl();
		JaxWsServerFactoryBean bean = new JaxWsServerFactoryBean();
		//Endpoint:地址
		bean.setAddress("http://127.0.0.1:9999/ws/cxf/languageService");
		bean.setServiceClass(LanduageService.class);//對外提供服務的業務類或接口
		bean.setServiceBean(landuageService);//服務的實現bean
		
		//添加輸入攔截器:輸入顯示日誌信息的攔截器
		bean.getInInterceptors().add(new LoggingInInterceptor());
		//添加輸出攔截器:輸出顯示日誌信息的攔截器
		bean.getInInterceptors().add(new LoggingOutInterceptor());
		
		bean.create();//建立,發佈webservice
		System.out.println("wsdl地址:http://127.0.0.1:9999/ws/cxf/languageService?WSDL");
	}
}

package cn.it.ws.cxf.a;

public class Test {
	/**
	 * 測試有CXF框架發佈的webservice服務
	 * @param args
	 */
	public static void main(String[] args) {
		LanguageManager ws = new LanguageManager();
		LanduageService landuageServicePort = ws.getLanduageServicePort();
		System.out.println(landuageServicePort.getLanguage(1));
	}
}
相關文章
相關標籤/搜索