webservice:web 服务,一种跨编程语言跨操作系统的远程调用技术
- 用途:主要用于多个异构系统的数据交互
- 分类:
- SOAP架构的webservice:使用 SOAP 协议
- Restful架构的webservice:使用 Http 协议
- SOAP架构的webservice:使用 SOAP 协议
SOAP 三要素
1、SOAP协议:Simple Object Access Protocol ,简单对象访问协议,一种基于XML的协议,它使应用程序通过http协议来交换数据
- SOAP = Http + xml
- 常用版本:soap 1.1 和 soap 1.2
2、WSDL:web service description language,基于xml的用于描述webservice的函数、参数、返回值等信息,通俗理解为 webservice 的使用说明书
3、UDDI:Universal Description,Discovery and Integration,一种目录服务,企业用它对web services进行注册和搜索,从而达到资源共享,促进全球经济合作
简单开发使用
使用 java 发布一个 webservice 服务端:
1、编写服务接口和其实现类,并用 @webService 进行注解
// 要发布的服务接口@WebServicepublic interface WeatherService {String queryWeather(String cityName);}// 服务实现@WebServicepublic class WeatherServiceImpl implements WeatherService {@Overridepublic String queryWeather(String cityName) {System.out.println("当前城市:" + cityName);if ("上海".equals(cityName)) {return "晴天";}return "其他地区未知";}}
2、发布服务:
Endpoint.publish(url , 服务实现类)
public class WeatherPublisher {public static void main(String[] args) {// 发布服务:服务地址 服务实现类实例// 发布服务后,自动生成说明书,说明书地址:服务地址?wsdlEndpoint.publish("http://localhost:8080/weatherService", new WeatherServiceImpl());}}
3、获取webservice说明书:
发布成功后,会自动生成webservice说明书,地址为:发布地址?wsdl
- 在浏览器输入:http://localhost:8080/weatherService?wsdl
webservice 客户端:
wsdl 说明书阅读:从下往上
- 先找到服务视图名称
,再在 service 标签下的 port 标签获取 binding 属性 - 通过服务视图名称找到相关的 binding :
- 由 type 属性找到相应的 portType 标签
,这就是服务的核心类,webservice 中所有的服务调用都在这里
根据 wsdl 生成客户端调用代码:wsimport -d . -s . -p 包名 wsdl地址
- -d:生成 .class 文件存放的未知,上面使用了相对路径 .
- -s:生成 .class 对应的源代码
- -p:指定生成文件的包名
- wsdl:文档地址,即 http://localhost:8080/weatherService?wsdl
将生成的 .java 文件导入项目(放在 -p 参数指定的包名中),然后通过如下的代码进行使用:
public class Main {public static void main(String[] args) {// 进行客户端调用// 创建服务视图名称对象WeatherServiceImplService weatherServiceImplService = new WeatherServiceImplService();// 通过服务视图名称对象找到binding---直接通过binding找到portType(服务核心类对象)WeatherServiceImpl weatherServiceImplPort = weatherServiceImplService.getWeatherServiceImplPort();// 调用方法String weather = weatherServiceImplPort.queryWeather("上海");System.out.println(weather);}}
spring 整合 jax-ws 开发 webservice
服务端:
1、导入相关依赖:
<!-- CSF WS开发核心 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.4.2</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>3.4.2</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>3.4.2</version><scope>test</scope></dependency><!-- spring webmvc --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.3.1</version></dependency>
2、配置 web.xml
<!-- spring配置文件位置 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-cxf.xml</param-value></context-param><!-- 加载spring工厂配置文件 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置cxf的servlet --><servlet><servlet-name>cxf</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class></servlet><servlet-mapping><servlet-name>cxf</servlet-name><url-pattern>/services/*</url-pattern></servlet-mapping>
3、创建 SEI(Service Endpoint Interface)服务发布接口:
@WebServicepublic interface WeatherService {String query(String location);}
4、创建 SEI 实现类
@WebServicepublic class WeatherServiceImpl implements WeatherService {@Overridepublic String query(String location) {System.out.println("当前城市:" + location);if ("上海".equals(location)) {return "晴天";}return "其他地区未知";}}
4、使用 Spring 整合 CXF 发布 webservice,配置 spring-cxf.xml
<!-- address:客户端访问服务器路径 --><!-- serviceClass:配置接口 --><!-- serviceBean:配置实现 --><jaxws:server id="weatherService" address="/weather" serviceClass="org.example.service.WeatherService"><jaxws:serviceBean><bean class="org.example.service.impl.WeatherServiceImpl"/></jaxws:serviceBean></jaxws:server>
