1、什么是WebService?
WebService是一种跨编程语言和跨操作系统平台的远程调用技术。
可以从多个角度来理解WebService,从表面上看,WebService就是一个应用程序向外界暴露出一个能通过Web进行调用的API,也就是说能用编程的方法通过Web来调用这个应用程序。我们把调用这个WebService的应用程序叫做客户端,而把提供这个WebService的应用程序叫做服务端。从深层次看,WebService是建立可互操作的分布式应用程序的新平台,是一个平台,是一套标准。它定义了应用程序如何在Web上实现互操作性,你可以用任何你喜欢的语言,在任何你喜欢的平台上写Web service ,只要我们可以通过Web service标准对这些服务进行查询和访问。
WebService平台需要一套协议来实现分布式应用程序的创建。任何平台都有它的数据表示方法和类型系统。要实现互操作性,WebService平台必须提供一套标准的类型系统,用于沟通不同平台、编程语言和组件模型中的不同类型系统。Web service平台必须提供一种标准来描述Web service,让客户可以得到足够的信息来调用这个Web service。最后,我们还必须有一种方法来对这个Web service进行远程调用,这种方法实际是一种远程过程调用协议(RPC)。为了达到互操作性,这种RPC协议还必须与平台和编程语言无关。
2、WebService的Java框架实现方式之一:CXF
Spring整合CXF
1、pom.xml中添加maven依赖
<!--cxf start-->
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-core -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
<!--cxf end-->
2、web.xml 中定义cxf的servlet
<!--定义一个cxf的servlet-->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
之后只要我们访问webservice/*这个地址就会进入CXF的servlet中。
3、编写系统要发布的服务接口,以及实现该接口的服务实现类以及方法
服务接口:
package com.bsk.cxf;
public interface HelloWorld {
String say(String string);
}
实现类:
package com.bsk.cxf.impl;
import javax.jws.WebService;
import org.springframework.stereotype.Component;
import com.bsk.cxf.HelloWorld;
@Component("helloWorld")
@WebService
public class HelloWorldImpl implements HelloWorld{
@Override
public String say(String string) {
return "Hello" + string;
}
}
4、Spring整合CXF
在applicationContext.xml中,对CXF进行配置
首先由于需要使用到CXF的标签,所以需要添加额外的命名路径如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
其次添加如下配置
<!-- cxf start -->
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<!-- 自动扫描webService -->
<context:component-scan base-package="com.bsk.cxf" />
<!-- 定义webservice的发布接口 -->
<jaxws:endpoint
implementor="#helloWorld"
address="/HelloWorld"
></jaxws:endpoint>
<!-- cxf end -->
#helloWorld
指的是我们在HelloWorldImpl
类中所自定义的名字,/HelloWorld
则是我们需要访问的地址。
测试结果:
运行项目输入地址,我的地址http://localhost:8080/spring-mybatis-redis-webservice/webservice/
点击上图中的链接 http://impl.cxf.bsk.com/}HelloWorldImplService,如果出现如下界面
则证明我们的webservice发布成功了。
接下来只需要通过客户端调用这个接口即可获得返回结果了。
参考:
https://crossoverjie.top/2016/08/02/SSM4/
https://www.cnblogs.com/Eillot/archive/2018/03/16/8583964.html