Java调用WebService的几种方式

webservice服务一般都是使用WSDL(web service descriptive language)文件的样式来发布的,在WSDL文件里面,包含这个webservice暴露在外面可供使用的接口。

wsdl文本解析

1、直接AXIS调用远程的web service

第一步:引入相关依赖

 <dependencies>
        <!--spring web Service的包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
            <version>1.5.2.RELEASE</version>
        </dependency>

        <!--spring web service wsdl包-->
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.3</version>
        </dependency>
        <!-- axis 1.4 jar start -->
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.2</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis-jaxrpc</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis-saaj</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.4</version>
        </dependency>
    </dependencies>

第二步:写代码

package demo;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.handlers.soap.SOAPService;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.nio.charset.StandardCharsets;
import java.rmi.RemoteException;


public class Test04 {

    public static void main(String[] args) throws RemoteException, ServiceException, MalformedURLException {
        //wsdl地址
        String endpoint ="http://localhost:57772/soap/JHIPLIB.SOAP.BS.HL7V3Service.cls";
        //命名空间
        String namespace = "http://goodwillcis.com";
        //服务名
        String serviceName = "HL7V3Service";
        //方法名
        String methodName = "HIPMessageServer";
        //soapAction
        String soapAction = "http://goodwillcis.com/JHIPLIB.SOAP.BS.HL7V3Service.HIPMessageServer";


        Service service = new Service();
        Call call = (Call) service.createCall();
        //设置响应超时
        call.setTimeout(3000);

        //设置地址
        call.setTargetEndpointAddress(endpoint);

        //设置方法名
        call.setOperationName(new QName(namespace,methodName));

       
        //设置参数
        call.addParameter("Message", XMLType.XSD_STRING, ParameterMode.IN);
        String message = new String("<req>测试</req>");
        Object [] obj = {message};
    
        //设置返回类型
        call.setReturnType(XMLType.XSD_STRING);

        //启用soap
        call.setUseSOAPAction(true);
        //设置soapAction
        call.setSOAPActionURI(soapAction);

        //设置服务名
        SOAPService soapService = new SOAPService();
        soapService.setName(serviceName);
        call.setSOAPService(soapService);


        String ret = (String) call.invoke(obj);
        System.out.println("返回消息:"+ret);


    }


}

2、采用代理类形式调用webservice

第一步:直接引用wsdl地址(或者wsdl文本)生成相应的代理类。

 

 

第二步:代码调用。

public static void main(String[] args) throws MalformedURLException {
        HL7V3Service service = new HL7V3Service();
        HL7V3ServiceSoap soap = service.getHL7V3ServiceSoap();
        String msg = "<req>3333</req>";
        String result = soap.hipMessageServer(msg);
        System.out.println(result);
    }

 

参考:https://blog.csdn.net/qq_35124535/article/details/62226585


版权声明:本文为wangli1281原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。