cxf webService简单例子

cxf发布webservice简单例子:
cxf最小依赖的jar:
cxf-2.2.6.jar
jetty-6.1.21.jar
jetty-util-6.1.21.jar
wsdl4j-1.6.2.jar
XmlSchema-1.4.5.jar
附件中为以上jar

接口类:
import javax.jws.WebService;

@WebService
public interface IHelloWorld {
	String sayHello();
}


实现类:
import javax.jws.WebService;

//@WebService(endpointInterface = "com.ddch.IHelloWorld", serviceName = "HelloWorld")
// 上面的可要可不要
public class HelloWorldImpl implements IHelloWorld {

	public String sayHello() {
		System.out.println("hello world call");
		return "Hello World!";
	}

}


服务器:
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class SoapServer {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		IHelloWorld impl = new HelloWorldImpl();
		JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
		factoryBean.setAddress("http://localhost:8089/helloworld");
		factoryBean.setServiceClass(IHelloWorld.class);
		//接口类
		factoryBean.setServiceBean(impl);
		factoryBean.create();
		System.out.println("WS发布成功!");
		
		// Endpoint.publish("http://localhost:8089/helloworld", new HelloWorldImpl());
		
	}
}


运行后浏览器中输入http://localhost:8089/helloworld?wsdl返回xml页面说明成功

如果url中不加wsdl参数则报错:org.apache.cxf.interceptor.Fault: No such operation

客户端调用:
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

public class WebServiceClient {

	public static void main(String[] args) throws Exception{
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
		Client client = dcf.createClient("http://localhost:8089/helloworld?wsdl");
		
		Object[] strs = client.invoke("sayHello");
		
		System.out.println(strs[0]);
	}
}


参考资料:http://wenku.baidu.com/view/2464a0272f60ddccda38a03f.html

你可能感兴趣的:(webservice)