1. 程式人生 > >CXF框架學習,釋出webservice服務

CXF框架學習,釋出webservice服務

筆記:

CXF框架,釋出webservice服務,並使用客戶端遠端訪問WebserviceApache CXF是一個開園的Services框架,CXF幫助您來構建和開發 Services這些Services可以支援多種協議,比如SOAP、POST/HTTP、RESTful HTTP CXF大大簡化了Service可以天然的和Spring進行無縫整合1.CXF介紹:soa框架是Celtrix(ESB框架)和XFile(webservice)合併而成,並且捐給了apache核心是org.apache.cxf.Bus(匯流排),類似於Spring的ApplicationContext預設是依賴於SpringApache CXF發行包中的jar,如果全部放到lib中,需要JDK1.6及以上,否則會報JAX-WS版本不一致的問題CXF內建了Jetty伺服器,他是servlet容器,好比tomcat2.CXF特點與Spring、Servlet做了無縫對接,CXF框架裡面集成了Servlet容器Jetty;支援註解的方式來發布WebService能夠顯示webservice的服務列表能夠新增攔截器:輸入攔截器、輸出攔截器、輸入日誌資訊攔截器、輸出日誌攔截器、使用者許可權認證的攔截器3.配置CXF--客戶端訪問釋出:org.apache.cxf.frontend.ServerFactoryBean;//不夠規範org.apache.cxf.jaxws.JaxWsServerFactoryBean;//可以用註解 推薦

4.jar包地址:http://download.csdn.net/download/qq510372845/9988794

也可以到apache官網下載

例子:實現:根據排名查詢開發語言

package com.zhijiao.lkm.language;

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

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

	/**
	 * 可以修改返回型別、方法明、方法引數
	 * @param position
	 * @return
	 */
	//@WebMethod(operationName="getlang")
	public @WebResult(name="languageResult")String  getLanguage(@WebParam(name="position")int position);

}
package com.zhijiao.lkm.language;

import org.apache.cxf.frontend.ServerFactoryBean;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

/**
 * 開發語言排行榜描述服務
 * @author lenovo
 *
 */
public class LanguageServiceImpl implements LanguageService {

	/* (non-Javadoc)
	 * @see com.zhijiao.lkm.language.LanguageService#getLanguage(int)
	 */
	@Override
	public String getLanguage(int postion) {
		String language = null;
		switch (postion) {
		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框架 ServerFactoryBean 釋出webservice
	 * @param address
	 * @param languageService
	 */
	private void testWebService1(String address,LanguageService languageService) {
		
		ServerFactoryBean serverFactoryBean = new ServerFactoryBean();
		serverFactoryBean.setAddress(address);//
		serverFactoryBean.setServiceClass(LanguageService.class);//對外提供webservice的業務類或者介面,有介面的就寫介面,沒有介面的就寫類
		serverFactoryBean.setServiceBean(languageService);//服務的實現bean
		serverFactoryBean.create();
		System.out.println("地址:"+address);
	}
	/**
	 * 通過  框架釋出webservice
	 * @param address
	 * @param languageService
	 */
	private void testWebService2(String address,LanguageService languageService) {
		JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean();
		jaxWsServerFactoryBean.setAddress(address);
		jaxWsServerFactoryBean.setServiceClass(LanguageService.class);
		jaxWsServerFactoryBean.setServiceBean(languageService);
		jaxWsServerFactoryBean.create();
		System.out.println("地址:"+address);
	}
	/** 通過cxf框架釋出webservice
	 * 1.ServerFactoryBean
	 *   --不設定註解就可以釋出webservice服務,但是不夠規範,不支援註解,不可以通過註解的方式來修改wsdl在標籤資訊
	 *   --不支援攔截器在新增
	 * 2.JaxWsServerFactoryBean
	 * @param args
	 */
	public static void main(String[] args) {
		
		LanguageService languageService = new LanguageServiceImpl();
		String address="http://127.0.0.1:8080/ws/cxf/languangeService";
		
		LanguageServiceImpl service = new LanguageServiceImpl();
		//service.testWebService1(address,languageService);
		service.testWebService2("http://127.0.0.1:8080/ws/cxf/languangeService2",languageService);
	}
}