Write webService client through cxf

The jar package of cxf uses version 2.7.0, and the code corresponding to pom.xml is posted here

<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>


The following code adds a proxy server, because the company server cannot directly access the webservice server corresponding to the external network
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();

//The service address of the WebService of the external network

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

//The parameter is the class object of the class where the method to be used is located

// TbalWebServiceService The java is reversely generated by the soupUI tool through the wsdl file of the server, which can be downloaded from Baidu

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( //Proxy server IP );
hcp.setProxyServerPort( //Proxy server port );
http.setClient(hcp);

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

 

// If you don't use a proxy server, remove the above code and use the code with the following two lines of comments.

// 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;
}
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326660028&siteId=291194637