WebService简介
WebService
是一种跨编程语言和跨操作系统平台的远程调用技术。 跨编程语言和跨操作平台 就是说服务端程序采用java编写,客户端程序则可以采用其他编程语言编写,反之亦然!跨操作系统平台则是指服务端程序和客户端程序可以在不同的操作系统上运行。
远程调用 就是一台计算机a上的一个程序可以调用到另外一台计算机b上的一个对象的方法,譬如,银联提供给商场的pos刷卡系统,商场的POS机转账调用的转账方法的代码其实是跑在银行服务器上。
再比如,amazon,天气预报系统,淘宝网,校内网,百度等把自己的系统服务以WebService服务的形式暴露出来,让第三方网站和程序可以调用这些服务功能,这样扩展了自己系统的市场占有率。
SOAP 协议 (简单对象访问协议Simple Object Access Protocol):
WebService通过HTTP协议发送请求和接收结果时,发送的请求内容和结果内容都采用XML格式封装,并增加了一些特定的HTTP消息头,以说明HTTP消息的内容格式,这些特定的HTTP消息头和XML内容格式就是SOAP协议
分解:SOAP协议 = HTTP协议 + XML数据格式 而CXF就是apache 下的 WebService 的开源框架。上手Demo
别的先不多说,直接写一个模拟远程调用天气预报的一个案例来进一步了解上面简单说了一下远程调用所用到的领域,这里再详细说一下,以“hao123”为例

下面就先模拟这种远程调用(这里不是真的天气预报,只是实现模拟远程调用的过程,返回的数据是我定义的死数据),话不多说,上代码
服务端代码--创建属性为war的maven工程
- 添加服务端的maven依赖
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.10</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
</dependencies>
- 接口类:IWeatherService
给接口添加@WebService注解
import javax.jws.WebService;
@WebService
public interface IWeatherService {
/**
* 查询城市的天气
*/
public String info(String city);
}
- 实现类:WeatherService
这里定义了死数据:如果是安徽这个城市则返回 “晴:26℃|31℃/14℃|体感温度 25℃”
其他的返回的都是 “多云:20℃|25℃/10℃|体感温度 18℃”
public class WeatherService implements IWeatherService {
@Override
public String info(String city) {
return "安徽".equals(city)?"晴:26℃|31℃/14℃|体感温度 25℃":"多云:20℃|25℃/10℃|体感温度 18℃";
}
}
- 配置web.xml和spring配置文件
<!-- 启动Spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<!-- 加载cxf servlet -->
<filter>
<filter-name>cxf</filter-name>
<filter-class>org.apache.cxf.transport.servlet.CXFServlet</filter-class>
</filter>
<filter-mapping>
<filter-name>cxf</filter-name>
<url-pattern>/ws/*</url-pattern>
</filter-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 服务端调用的类,交给spring -->
<bean id="weatherService" class="com.muzile.cxf.impl.WeatherService"></bean>
<!-- 发布服务,cxf获得spring容器获得bean -->
<jaxws:server address="/weatherService">
<jaxws:serviceBean>
<ref bean="weatherService"/>
</jaxws:serviceBean>
</jaxws:server>
</beans>
通过访问/ws/*的路径下的资源,cxf获得spring容器,调用cxf配置下的bean
- 运行服务端,在浏览器中输入地址:类似http://localhost:9090/cxf/ws/weatherService?wsdl
输入地址最后的参数WSDL是:基于XML的语言,用于描述Web Service及其函数、参数和返回值。它是WebService客户端和服务器端都能理解的标准格式。因为是基于XML的,所以WSDL既是机器可阅读的,又是人可阅读的,这将是一个很大的好处。
客户端代码–创建属性为jar的maven工程
通过cmd生成客户端代码
客户端的src/main/java中获得一下目录结构
- 添加客户端的maven依赖
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.10</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- 用于单元测试,可选填-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
- 配置spring配置文件
jaxws:client中address属性的值为获得wsdl的路径,serviceClass为接口类全名(非实现类)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:client id="weatherClient" address="http://localhost:9090/cxf/ws/weatherService?wsdl"
serviceClass="com.muzile.cxf.impl.IWeatherService"></jaxws:client>
</beans>
- 最后编写测试类
package com.muzile.cxf.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.muzile.cxf.impl.IWeatherService;
public class CxfTest {
@Test
public void clientTest(){
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext_cxf.xml");
IWeatherService ws = (IWeatherService) ac.getBean("weatherClient");
System.out.println(ws.info("安徽"));
System.out.println(ws.info("南京"));
}
}
- 输出结果
最后测试的结果:客户端成功的调用了服务端的获得天气信息的方法