Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services, 像 JAX-WS 。这些 Services 可以支持多种协议 Ba la Ba la….编不下去了,具体的介绍请百度 首先在C
Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services, 像 JAX-WS 。这些 Services 可以支持多种协议 Ba la Ba la….编不下去了,具体的介绍请百度
首先在CXF 2.7.18的压缩包,地址:http://cxf.apache.org/download.html
解压后把lib目录下所有的jar包导入到项目中,注意有个WHICH_JARS的文件,不要导
服务端代码实现:
@WebService(targetNamespace="http://push.yonyou.com",
serviceName="PushData",
portName="PushPort",
name="PushPortType")
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface PushData {
public @WebResult(name="pk_orgs")List<String> queryOrgs(@WebParam(name="corp")String corp);
}
public class PushDataImp implements PushData{
@Override
public List<String> queryOrgs(String corp) {
List<String> orgs = new ArrayList<String>();
orgs.add(0,"10011AFSD10");
orgs.add(1,"10011A87D10");
orgs.add(2,"10011A09E10");
return orgs;
}
}
发布服务
public class Publish {
public static void main(String[] args) {
//jaxws服务工厂
JaxWsServerFactoryBean facatory = new JaxWsServerFactoryBean();
//设置服务接口类
facatory.setServiceClass(PushData.class);
//设置服务实现类
facatory.setServiceBean(new PushDataImp());
//设置地址
facatory.setAddress("http://10.101.238.173:15008/push");
//发布
facatory.create();
}
}
网址中输入:http://10.101.238.173:15008/push?wsdl 检查是否发布成功
因为我使用的JDK1.6.所以使用wsdl2java命令生成客户端java代码
wsdl2java -d . -frontend jaxws21 http://10.101.238.173:15008/push?wsdl
生成的代码如下:
客户端代码调用
客户端代码的调用同样有两种,一个是CXF的API,一个是通用的Service调用,可以指定namespace,servicename等
public class Client {
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(PushPortType.class);
factory.setAddress("http://10.101.238.173:15008/push");
PushPortType port = (PushPortType) factory.create();
List<String> queryOrgs = port.queryOrgs("ax");
if(queryOrgs != null && queryOrgs.size() > 0){
for (String org : queryOrgs) {
System.out.println(org);
}
}
}
}
或者
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://10.101.238.173:15008/push?wsdl");
QName qName = new QName("http://push.yonyou.com", "PushData");
Service service = Service.create(url, qName);
PushPortType port = service.getPort(PushPortType.class);
List<String> queryOrgs = port.queryOrgs("ax");
for (String org : queryOrgs) {
System.out.println(org);
}
}
输入结果如下:
2016-12-23 11:29:38 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://push.yonyou.com}PushPortTypeService from class com.yonyou.push.PushPortType
10011AFSD10
10011A87D10
10011A09E10