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);
	}
}



你可能感兴趣的:(Web,Service)