springboot项目如何调用webservice-soap接口

前端 0

首先基于soap协议的传输的话,是基于类似于xml这样的wsdl格式进行传输的

        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>            <version>3.3.4</version>            <exclusions>                <exclusion>                    <groupId>javax.validation</groupId>                    <artifactId>validation-api</artifactId>                </exclusion>                <exclusion>                    <groupId>org.springframework.boot</groupId>                    <artifactId>spring-boot-starter</artifactId>                </exclusion>                <exclusion>                    <groupId>org.springframework.boot</groupId>                    <artifactId>spring-boot-starter-web</artifactId>                </exclusion>            </exclusions>        </dependency>

如果是springboot项目一定要把里边的这几个依赖排除掉,否则因为已经引入过对应的依赖了,在启动的时候会发生冲突

下边介绍两种方式:

一,首先是基于cxf的动态代理的方式

 JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();        Client client = dcf.createClient(wsdl接口路径);        Object[] objects = client.invoke("方法名称",方法参数);       objects[0]就是这个方法的返回值//这里注意,如果是传递是一个对象的话,一定要注意传递的类的全路径一定要和服务端的类全路径一致,否则,会出现接受错误

二,通过java的wsimport生成客户端代码的方式,命令如下

wsimport  -s  客户端代码生成全路径   -p  代码的包路径   请求接口地址

 然后就可以通过下边这种调用方法这样的方式直接拿到调用的返回值

 AlarmHandleService alarmHandleService=new AlarmHandleService();        AlarmHandleServiceSoap alarmHandleServiceSoap = alarmHandleService.getAlarmHandleServiceSoap();        ReturnMessage returnMessage = alarmHandleServiceSoap.alarmSend(alarmSendMessage);

也许您对下面的内容还感兴趣: