axis和axis2调用webservice接口

时间: 2023-08-15 admin 互联网

axis和axis2调用webservice接口

axis和axis2调用webservice接口

1.axis

<dependency>
   <groupId>axis</groupId>
   <artifactId>axis</artifactId>
   <version>1.4</version>
</dependency>
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.message.SOAPHeaderElement;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import java.rmi.RemoteException;


@SpringBootTest
class WebService02 {


    @Test
    public void testws03() {
        String reqXml = "请求报文";//xml或json等
        String url = "http://ip:port/xx/xxxxxxx?wsdl";
        Service serv = new Service();
        try {
            Call call = (Call) serv.createCall();
            call.addHeader(new SOAPHeaderElement("Authorization", "username", "xxxxxx"));
            call.addHeader(new SOAPHeaderElement("Authorization", "password", "******"));
            call.setTargetEndpointAddress(url);
            call.setOperationName(new QName(url, "方法名"));
            String errXML = (String) call.invoke(new Object[] { reqXml });
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            System.out.println("远程连接异常:" + e.toString());
            e.printStackTrace();
        } catch (Error er) {
            er.printStackTrace();
        }
    }

}

2.axis2

        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-local</artifactId>
            <version>1.7.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-http</artifactId>
            <version>1.7.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-adb</artifactId>
            <version>1.7.8</version>
        </dependency>

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.util.URL;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

@SpringBootTest
class WebService01 {

    @Test
    public void testws01() {
        try {
            String xml = "请求报文";
            ServiceClient serviceClient = new ServiceClient();
            //创建服务地址WebService的URL,注意不是WSDL的URL
            String url = "http://ip:port/xxx/xxxxxx?wsdl";
            EndpointReference targetEPR = new EndpointReference(url);
            Options options = serviceClient.getOptions();
            options.setTo(targetEPR);
            options.setAction("命名空间/方法名");

            OMFactory fac = OMAbstractFactory.getOMFactory();

            OMNamespace omNs = fac.createOMNamespace("命名空间", "");
            // 指定方法
            OMElement method = fac.createOMElement("方法名", omNs);
            // 指定方法的参数
            OMElement requestXml = fac.createOMElement("requestXml", omNs);
            requestXml.setText(xml);
            method.addChild(requestXml);
            method.build();
            //远程调用web服务
            OMElement result = serviceClient.sendReceive(method);
            System.out.println(result);

        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();
        }
    }

    @Test
    public void testws02(){
        String invokeUrl = "http://ip:port/xxxx/xxxx?wsdl";
        String reqXml = "参数报文";
        try {
            RPCServiceClient client = new RPCServiceClient();
            Options options = client.getOptions();
            options.setTimeOutInMilliSeconds(5000);
            EndpointReference epf = new EndpointReference(invokeUrl);
            options.setTo(epf);
            options.setProperty(HTTPConstants.CHUNKED, false);
            URL url = new URL(invokeUrl);

            OMFactory factory0 = OMAbstractFactory.getOMFactory();
            OMNamespace securityNS0 = factory0.createOMNamespace("Authorization", "ns1");
            OMElement param0 = factory0.createOMElement("username", securityNS0);
            param0.setText("用户名");
            client.addHeader(param0);

            OMFactory factory1 = OMAbstractFactory.getOMFactory();
            OMNamespace securityNS1 = factory1.createOMNamespace("Authorization", "ns1");
            OMElement param1 = factory1.createOMElement("password", securityNS1);
            param1.setText("密码****");
            client.addHeader(param1);


            OMFactory factory = OMAbstractFactory.getOMFactory();
            OMNamespace methodNS = factory.createOMNamespace("命名空间", "");
            OMNamespace argNS = factory.createOMNamespace("", "");
            OMElement method = factory.createOMElement("方法名", methodNS);
            List<String> methodArgs = new ArrayList<>();
            methodArgs.add(reqXml);
            for (int i = 0; i < methodArgs.size(); i++){
                String name = String.format("arg%s", i);
                String value = methodArgs.get(i);
                OMElement argElement = factory.createOMElement(name, argNS);
                argElement.setText(value);
                method.addChild(argElement);
            }
            method.build();
            OMElement result = client.sendReceive(method);
            String newResult = result.getFirstElement().getText();
        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();
        }
    }
}