`
jie66989
  • 浏览: 250576 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

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
  • lib.rar (4.9 MB)
  • 下载次数: 34
分享到:
评论
1 楼 zz210891470 2014-12-18  
会报这个错啊Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/xml/bind/marshaller/NamespacePrefixMapper

相关推荐

Global site tag (gtag.js) - Google Analytics