Springboot调用wsdl的webservice接口两种不常用方式

2022-02-10 14:49:31 浏览数 (1)

代码语言:javascript复制
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;
    }
    

0 人点赞