CXF调用接口共有两种方式,直接看代码:(以下两种方式都可以调用使用net的wcf写的接口)
1.动态代理
package com.demo.wcf;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import java.io.File;
import java.net.URL;
/**
* 可以调用C#的WCF的接口
*/
public class CxfDynamicCallWcf {
private static String apiUrl = "http://xxx/Service.svc?singleWsdl";
private static Client client = null; // 接口客户端
public static void main(String[] args) throws Exception {
initApi();
importData();
}
private static void initApi() {
File file = new File(System.getProperty("java.home"));
// 这个地方必须输出是jre才可以
System.out.println("file.getName()-->" + file.getName() + "file.getAbsolutePath()-->" + file.getAbsolutePath());
try {
if (client == null) {
// 接口地址
URL wsdlURL = new URL(apiUrl);
System.out.println("接口地址:" + apiUrl);
JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
// 1.报错空指针useJava6Compiler.... 1.解决:eclipse里设置java的环境为:设置为jdk里的jre才行, C:\Program
// Files\Java\jdk1.8.0_172 1.解决:生成环境里设置:应该读的是JAVA_HOME里的
//
// 2.报错:[java.lang.ClassNotFoundException: com/sun/tools/internal/xjc/api/XJC]
// 2.解决:原因是:JDK同级目录的那个JRE文件夹的lib下没有tools.jar,将JRE/lib/tools.jar复制到此。
client = factory.createClient(wsdlURL);
HTTPConduit conduit = (HTTPConduit) client.getConduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
long timeout = 10 * 60 * 1000;//
policy.setConnectionTimeout(timeout);
policy.setReceiveTimeout(timeout);
conduit.setClient(policy);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void importData() throws Exception {
// 调用 webservice
Object[] result = client.invoke("Get168latest");
System.out.println("result");
System.out.println(result);
if (result != null && result.length > 0) {
String s = result[0].toString();
System.out.println(s);
JSONArray objects = JSON.parseArray(s);
System.out.println(objects);
}
}
}
2.生成客户端代理
1.下载官方下载网址:
http://cxf.apache.org/download.html
windows系统(window 中选择zip)下要下载的内容:
2.解压apache-cxf-3.1.15.zip包,并进入这个apache-cxf-3.1.15目录下面的bin,进入cmd
生成命令
wsdl2java -p [生成代码的包目录] -d [代码生成的路径] -client -encoding utf-8 -noAddressBinding http://localhost:8099/chnInterWs/ChnInterServiceImpl?wsdl
例如
wsdl2java -p org.kspace.web.service.webservice -d E:\JavaCode\kspace-parent\kspace-jspui\src\main\java\org\kspace\web\service\webservice -client -encoding utf-8 -noAddressBinding
https://xxx/Service.svc?singleWsdl
3.客户端实现代码
package org.kspace.web.service;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.apache.log4j.Logger;
import org.kspace.base.config.local.LocalConfigManager;
import org.kspace.web.service.webservice.IService;
import java.net.URL;
public class GetWebServices {
private static final Logger log = Logger.getLogger(GetWebServices.class);
private static IService service = null;
public static IService getService() throws Exception {
if (service == null) {
URL url = new URL("https://xxx/Service.svc?singleWsdl");
service = (url == null ? new org.kspace.web.service.webservice.Service() : new org.kspace.web.service.webservice.Service(url)).getBasicHttpBindingIService();
Client client = ClientProxy.getClient(service);
// 设置客户端的配置信息,超时等.
HTTPConduit conduit = (HTTPConduit) client.getConduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setConnectionTimeout(6000000); // 连接超时时间秒
policy.setReceiveTimeout(6000000);// 请求超时时间.(读取超时)
conduit.setClient(policy);
}
return service;
}
}
4.调用接口
try {
//调用接口 http://xxx/Service.svc
String subject168 = GetWebServices.getService().get168Latest();
if (org.apache.commons.lang3.StringUtils.isNotBlank(subject168)) {
JSONArray objects = JSON.parseArray(subject168);
request.setAttribute("subject168List", objects);
}
} catch (Exception e) {
e.printStackTrace();
}
3.总结:两种调用方式各有优劣:
使用第1种“动态代理”方式
优点:每次接口有修改,不需要额外生成代理类。
缺点:生产环境由于"java.home"不同容易产生各种问题。
使用第2种“生成客户端代理”方式
优点:生产环境不会产生上述问题。
缺点:每次接口有修改,都需要重新生成代理类。