上篇文章xfire的使用中,我都已经介绍过webservice的概念的三个核心内容,并做了它的开发和调用,这里我就用同样的方式,来做一做cxf的。
第一步,下载apache-cxf-2.2.3.zip
第二步,新建一个java项目,把解压后cxf的lib下面的jar全都添加到项目的环境里
第三步,编写测试用接口和实现类,代码如下:
- package test;
-
- import javax.jws.WebService;
-
- public interface Hello {
- @WebService
- public String sayHello(String str);
-
- }
class HelloImpl implements Hello {
public String sayHello(String str) {
System.out.println("调用成功");
return "Hello " + str;
}
}
第四步,编写服务端的生成代码,代码如下:
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerfactorybean;
class MainServer {
static void main(String[] args) {
JaxWsServerfactorybean factory = new JaxWsServerfactorybean();
factory.setAddress("http://localhost:8080/HelloWebService");
factory.setServiceClass(HelloImpl.class);
Server server = factory.create();
server.start();
}
}