cxf webservice 客户端生成以及调用举例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Brady74/article/details/80338766

1. 安装cxf 客户端 apache-cxf-3.1.6.tar

直接解压,然后把目录配置到环境变量,安装完成

2. 通过我们安装的客户端和别人给出来的wsdl地址来生成客户端代码:

例:wsdl2java -p com.zzzl.webservice.qidian -d d:\cxfoutput\src -all http://game.qidian.com/RemoteWebService/IPreventIndulge.asmx?wsdl

参数说明:

 

-p 也就是package 对应java中的包

-d 输入目录,生成.java文件会在该目录,会自动添加-p参数配置的包路径

-client 生成客户端测试web service的代码.
-server 生成服务器启动web  service的代码.
-impl 生成web service的实现代码.
-ant  生成build.xml文件.
-all 生成上面-client -server -impl -ant 对应的所有文件.


3.根据生成的客户端代码,我们调用接口地址

例:

public static void main(String[] args) {

org.apache.cxf.jaxws.JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(ICreateComplainService.class);
String webServicesUrl3 = "http://localhost:8080/webservice/services/CreateComplainService";
factory.setAddress(webServicesUrl3);
ICreateComplainService client = (ICreateComplainService) factory.create();
String params = "{\" sourceCaseId \":123,\" id\":\"test!!!\",\" status \":\"1000\",\" content \":\"message in\"}";
String rs = client.addInfoOrEndByItsm(params);
System.out.println(rs);
}

例2(利用反射):

public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException {
org.apache.cxf.jaxws.JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
Class<?> demo = Class.forName("com.module.webservices.server.AddService");
Method method = demo.getMethod("addIncident", Map.class, String.class);
factory.setServiceClass(demo);
String sss = "http://localhost:8080/webservices/addNew";
factory.setAddress(sss);
Object client = factory.create();
String wuRenCang = "{\"SOURCEID\": \"9\",\"SOURCECASEID\": \"1111111\"}";
Map<String, DataHandler> dataHandleList = new HashMap<String, DataHandler>();
String path5 = "D:\\123.jpg";
String path = "D:\\systeminfo.txt";
String fileName = "testFile.txt";
String fileName2 = "testFiles.txt";
DataHandler handler = new DataHandler(new FileDataSource(path));
DataHandler handler3 = new DataHandler(new FileDataSource(path5));
dataHandleList.put(fileName, handler);
dataHandleList.put(fileName2, handler3);
String rs = (String) method.invoke(client, dataHandleList, wuRenCang);
System.out.println(rs);
}

例3(带验证的service接口调用):

public static void main(String []args) throws RemoteException{
String params = "{\"sourceCaseId\":\"005056A52D701ED780FA08B811B7E37E\"}";
String devEnvironment = "https://localhost:8080/sap/bc/srt/rfc/zsm_wb01";
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(ZSMWB01.class);
        factory.setAddress(devEnvironment);
        String userName = "root";
        String password = "123456";
        factory.setUsername(userName);
        factory.setPassword(password);
        ZSMWB01 serviceProxy = (ZSMWB01) factory.create();
        org.apache.cxf.endpoint.Client proxy = ClientProxy.getClient(serviceProxy);
        HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
        TLSClientParameters tls= new TLSClientParameters();
        tls.setTrustManagers( new TrustManager[]{ new DummyTrustManager()});
        tls.setDisableCNCheck(true);//接受服务器端的认证  
        conduit.setTlsClientParameters(tls);
        String zFmsmItsmSm02 = serviceProxy.zFmsmItsmSm02(params);
System.out.println(zFmsmItsmSm02);

}

辅助类:DummyTrustManager 

import java.security.cert.X509Certificate;


import javax.net.ssl.X509TrustManager;


public class DummyTrustManager implements X509TrustManager {
    public void checkClientTrusted( X509Certificate[] cert, String authType) {
        return;
    }
    
    public void checkServerTrusted( X509Certificate[] cert, String authType) {
        return;
    }
    
    public X509Certificate[] getAcceptedIssuers() {
        return new X509Certificate[0];
    }

}

额外添加:

Class<?> serviceClient = Class.forName(cfg.getServiceClassPath());
factory.setServiceClass(serviceClient);
factory.setAddress(cfg.getWsAddress());
boolean needSsl=false;
if(null != cfg.getSslUser() && !"".equals(cfg.getSslUser())){
needSsl=true;
}
if(needSsl){
factory.setUsername(cfg.getSslUser());
factory.setPassword(cfg.getSslPwd());
}

object = factory.create();

if (needSsl) {
Client proxy = ClientProxy.getClient(object);
HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
TLSClientParameters tls = new TLSClientParameters();
tls.setTrustManagers(new TrustManager[] { new DummyTrustManager() });
tls.setDisableCNCheck(true);// 接受服务器端的认证
conduit.setTlsClientParameters(tls);
}

refIncidentMap.put(sourceId, object);

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页