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