1.下载CXF
http://apache.etoak.com//cxf/2.3.0/apache-cxf-2.3.0.zip
CXF是XFire的升级半,XFire已经停止更新了
让后将lib下所有jar包(可能有些jar包不需要,还没研究)添加到BuildPath下
2.编写接口
写道
@WebService
public interface IHello {
public String sayHello(String word);
}
public interface IHello {
public String sayHello(String word);
}
@WebService 注解IHello发布成服务,同样其实现类也要添加
写道
@WebService
public class HelloImpl implements IHello {
public String sayHello(String word) {
System.out.println("receive client word"+word);
return "Good Luck!Client,"+word;
}
}
public class HelloImpl implements IHello {
public String sayHello(String word) {
System.out.println("receive client word"+word);
return "Good Luck!Client,"+word;
}
}
3.通过CXF内置Jetty Servelet容器发布服务
写道
public class MainServer {
public static void main(String[] args) {
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setServiceClass(HelloImpl.class);
factory.setAddress("http://localhost:8080/helloword");
Server server = factory.create();
server.start();
}
}
public static void main(String[] args) {
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setServiceClass(HelloImpl.class);
factory.setAddress("http://localhost:8080/helloword");
Server server = factory.create();
server.start();
}
}
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();---webService工厂类
factory.setServiceClass(HelloImpl.class);---一定是实现类,要不然客户端找不到的
factory.setAddress("http://localhost:8080/helloword");---通过该地址我们就可以访问我们服务
http://localhost:8080/helloword?wsdl 录入地址栏,回车,你就可以看到wsdl描述
4.客户端
客户端需要IHello接口,实际开发时,我们不需要关心我们需要什么接口,只要我们拿到WSDL文件,就可以把wsdl转换成代码(接口会自动生成)
public class Client {
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress("http://localhost:8080/helloword");
factory.setServiceClass(IHello.class);//绑定接口类
IHello hello = (IHello)factory.create();
System.out.println(hello.sayHello("How are you!"));
}
}