cxf也是java的webservice,而且比axis2强大的太多(上一篇文章,就是整理axis,哎呀,那个被整的纠结啊),今天看到了cxf,果断放弃axis!
1.首先,下载cxf
下载地址请戳我
2.新建web项目
写个接口和实现类
接口
@WebService public interface IReaderService { public Reader getReader(@WebParam(name="name")String name,@WebParam(name="password")String password); public List<Reader> getReaders(); }
实现类
@WebService(endpointInterface="com.zy.service.IReaderService",serviceName="readerService") public class ReaderServiceImp implements IReaderService{ @Override public Reader getReader(@WebParam(name="name")String name,@WebParam(name="password")String password) { return new Reader(name,password); } @Override public List<Reader> getReaders() { List<Reader> l = new ArrayList<Reader>(); l.add(new Reader("mike01","123")); l.add(new Reader("mike02","123")); return null; } }
public class Reader{ private static final long serialVersionUID = 1L; public Reader(){} private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Reader(String name, String password) { super(); this.name = name; this.password = password; } public String toString(){ return "Name:"+name+",Password:"+password; } }
3.测试部署是否成功
CXF自带了一个轻量的容器服务,相当于spring自己提供了IOC容器一样。我们可以先用它来测试一下我们部署成功没。
System.out.println("Service starting!"); ReaderServiceImp readerService = new ReaderServiceImp(); Endpoint.publish("http://localhost:8080/readerService",readerService); System.out.println("Service started!");
我们看到启动已经完成,接着启动浏览器看看是否成功了。直接在浏览器输入http://localhost:8080/readerService?wsdl,我们可以看到:
4.访问服务
ok,到这里,cxf的发布和访问都实现了。
但是,这样是可以实现效果,可我们在大部分情况,是希望发布和访问是在同一个系统的,那我们该如何解决呢?