一、webservice介绍
1.什么是webservice
webService是一种使用http传输SOAP协议数据的远程调用技术。
webservice是一种跨平台,跨语言的规范,用于不同平台,不同语言开发的应用之间的交互。
Web Service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的交互操作的应用程序。
Webservice的一个最基本的目的就是提供在各个不同平台的不同应用系统的协同工作能力。可以说,它就是一个可以远程调用的类,或者说是组件,可以 把你本地的功能开放出去共别人调用。
Web Service技术, 能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成。依据Web Service规范实施的应用之间, 无论它们所使用的语言、 平台或内部协议是什么, 都可以相互交换数据。
WebService是自描述、 自包含的可用网络模块, 可以执行具体的业务功能。Web Service也很容易部署, 因为它们基于一些常规的产业标准以及已有的一些技术,诸如标准通用标记语言下的子集XML、HTTP。
Web Service减少了应用接口的花费。Web Service为整个企业甚至多个组织之间的业务流程的集成提供了一个通用机制。
2.webservice三要素
SOAP:规范XML标签
WSDL:服务端的使用说明书
UDDI:目录
二、java调用webservice的几种方式
1.直接AXIS调用远程的webservice
第一步:引入相关依赖
<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>
第二步:写代码
在写代码之前必须能看懂XML格式的WSDL文件,找到如下具体参数
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地址,生成相应的代理类
①选择项目,点击Tools,选择WebServices的Grnerate Java Code From Wsdl;
②输入wsdl地址,选择生成代理类所在的包,选择服务点击OK;
③此时代码就已经拉取到项目中了;
注意:会生成很多文件,大体只需关注Soap.java文件;
第二步:代码调用
public class TestHegangCard {
public static void main(String[] args) {
//创建视图
BaiLingService baiLingService = new BaiLingService();
//获取服务实现类
IBaiLingService iBaiLingService = baiLingService.getPort(IBaiLingService.class);
//入参
String inValue = "<root><HospitalId>1</HospitalId><BeginTime>2022-05-01</BeginTime><EndTime>2022-05-23</EndTime></root>";
//调用queryHealthCard查询方法测试
String cardNum1 = iBaiLingService.queryHealthCard(inValue);
}
}
具体其他创建等操作查看
https://blog.csdn.net/qq_35124535/article/details/62226585