基于CFX WebService做的一个简单的HelloWorld例子

package test;

import javax.jws.WebService;

@WebService
public interface HelloWorld {

	public String sayHello(String name);
}
package test;

import javax.jws.WebService;

@WebService
public class HelloWorldImpl implements HelloWorld {

	public String sayHello(String name) {
		System.out.println("sayHello is called");
		return "Hello "+name;
	}

}
package test;

import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class MainServer {

	
	public static void main(String[] args) {
		
		JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
		HelloWorld hello = new HelloWorldImpl();
		factoryBean.setServiceBean(hello);
		factoryBean.setServiceClass(HelloWorldImpl.class);
		factoryBean.setAddress("http://127.0.0.1:8080/HelloWorld");
		
		Server server = factoryBean.create();
		server.start();
	}

}


package test;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class HelloWorldClient {

	
	public static void main(String[] args) {

		JaxWsProxyFactoryBean bean = new JaxWsProxyFactoryBean();
		bean.setAddress("http://127.0.0.1:8080/HelloWorld");
		bean.setServiceClass(HelloWorld.class);
		
		HelloWorld world = (HelloWorld) bean.create();
		System.out.println(world.sayHello("LCY"));
	}

}

注:网上有些视频例子里面给的MainServer类里面 只写了factoryBean.setServiceClass(HelloWorldImpl.class); 

应该还要再前面加上

HelloWorld hello = new HelloWorldImpl();

factoryBean.setServiceBean(hello);

不然会报异常,异常情况见:http://topic.csdn.net/u/20110809/15/a39454e4-4c73-4ef6-9a34-5b30608eed4f.html?62533

解决问题来源于:http://www.opensourceforce.org/forum/viewthread.php?action=printable&tid=2613

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值