通过cxf来写webService客户端

cxf的jar包使用的是2.7.0的版本,pom.xml对应的代码这边贴出来

<dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-xjc</artifactId>
            <version>2.2.1.1</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.1.1</version>
        </dependency>


下列代码增加了代理服务器,因为公司服务器不能直接访问外网对应的webservice服务器
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.dcits.common.util.Base64Util;
import com.dcits.common.util.PropertiesUtil;


public class TClient {
protected final static Logger logger = LoggerFactory.getLogger(TClient.class);
public static String getReport(String barCode, String mobilePhone,
String patientName, String idCardNum) {
try {

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

//外网的WebService的服务地址

factory.setAddress("http://113.108.207.92:7001/PG/webservice/tbalWebService?wsdl");

//参数是要用的方法所在类的class对象

//TbalWebServiceService该java是通过服务端的wsdl文件用soupUI工具来反向生成的,可以百度下

factory.setServiceClass(TbalWebServiceService.class);


TbalWebServiceService pspt = (TbalWebServiceService) factory.create();


Client client = ClientProxy.getClient(pspt);
HTTPConduit http = (HTTPConduit) client.getConduit();
HTTPClientPolicy hcp = new HTTPClientPolicy();
hcp.setProxyServer(//代理服务器IP);
hcp.setProxyServerPort(//代理服务器端口);
http.setClient(hcp);

String userCode = Base64Util.getBase64("tbal");
String password = Base64Util.getBase64("tbal123");

 

//  如果不使用代理服务器,上面代码去掉,使用下面两行注释的代码就可以了

// JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
// Client client = dcf.createClient("http://113.108.207.92:7001/PG/webservice/tbalWebService?wsdl");
Object[] object = client.invoke("login", userCode, password);
String objStr = Base64Util.getFromBase64((String) object[0]);
JSONObject json = new JSONObject(objStr.toString());
JSONObject o = (JSONObject) json.get("data");
String uuid = Base64Util.getBase64((String) o.get("uuid"));
String barCode64 = Base64Util.getBase64(barCode);
Object[] objects = client.invoke("getReportInfo", uuid, barCode64,
mobilePhone, patientName, idCardNum);
String obj = Base64Util.getFromBase64((String) objects[0]);
JSONObject jsonReport = new JSONObject(obj.toString());
String code = (String)jsonReport.get("code");
if("0".equals(code))
{
JSONObject reportData = (JSONObject) jsonReport.get("data");
JSONArray reportList = (JSONArray) reportData.get("reportList");
JSONObject report = (JSONObject) reportList.get(0);
String pdfUrl = (String) report.get("pdfUrl");
return pdfUrl;
}
else
{
return null;
}
} catch (Exception e) {
logger.error("",e);
}
return null;
}
}

猜你喜欢

转载自blog.csdn.net/weixin_40157571/article/details/78865408