1. 程式人生 > 實用技巧 >Springboot呼叫wsdl的webservice介面兩種不常用方式

Springboot呼叫wsdl的webservice介面兩種不常用方式

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;    

public String sendXml(String xml) { // 建立動態客戶端 JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient(System.getProperty("wsdl")); // 需要密碼的情況需要加上使用者名稱和密碼 // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
Object[] objects = new Object[0]; try { // invoke("方法名",引數1,引數2,引數3....); objects = client.invoke("getUserName", xml); logger.debug("返回資料:" + objects[0]); return objects[0].toString(); } catch (java.lang.Exception e) { e.printStackTrace(); }
return null; } public String sendXml2(String xml) throws Throwable { String url = System.getProperty("wsdl"); Service serv = new Service(); Call call = (Call) serv.createCall(); call.setTargetEndpointAddress(url); call.setOperationName(new QName("http://webservice.rpc.other.web.demo.g4studio.org/","createsite")); call.addParameter(new QName("http://webservice.rpc.other.web.demo.g4studio.org/", "xmlContent"), XMLType.XSD_STRING, Class.forName("java.lang.String"), ParameterMode.IN ); call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING); System.out.println("推送的xml---"+xml); String str=(String) call.invoke(new Object[] {xml}); System.out.println(str+"----------------推送成功"); return str; }