Java CXF框架(案例加解析)(快速入门)模拟服务端和客户端(客户端远程调用服务端功能)

CXF框架

一.WebService与CXF简介

1、WebService概述

WebService是一种跨编程语言跨操作系统平台的远程调用技术。
跨编程语言和跨操作平台 就是说服务端程序采用java编写,客户端程序则可以采用其他编程语言编写,反之亦然!
跨操作系统平台则是指服务端程序和客户端程序可以在不同的操作系统上运行。

远程调用 就是一台计算机a上的一个程序可以调用到另外一台计算机b上的一个对象的方法,譬如,银联提供给商场的pos刷卡系统,商场的POS机转账调用的转账方法的代码其实是跑在银行服务器上。

再比如,amazon,天气预报系统,淘宝网,校内网,百度等把自己的系统服务以WebService服务的形式暴露出来,让第三方网站和程序可以调用这些服务功能,这样扩展了自己系统的市场占有率。

服务端:把公司内部系统的业务方法发布成WebService服务,供远程他人调用

客户端:调用别人发布的WebService服务

1.1常见的远程调动技术:

(1)Socket 套接字 TCP/IP UDP
(2)WebService
(3)http 调用
(4)RMI( 远程方法调用 ) Hessian 框架(二进制RPC协议传输数据)

1.2、WebService 的特点:

(1)跨平台,跨语言
(2)W3C(万维网联盟)制定的标准
(3)可以穿透防火墙(因为 soap 协议是基于 HTTP 协议)

SOAP 协议(简单对象访问协议Simple Object Access Protocol):
WebService通过HTTP协议发送请求和接收结果时,发送的请求内容和结果内容都采用XML格式封装,并增加了一些特定的HTTP消息头,以说明HTTP消息的内容格式,这些特定的HTTP消息头和XML内容格式就是SOAP协议

SOAP协议 = HTTP协议 + XML数据格式
WSDL

(Web Services Description Language)就是基于XML的语言,用于描述Web Service及其函数、参数和返回值。

它是WebService客户端和服务器端都能理解的标准格式。

因为是基于XML的,所以WSDL既是机器可阅读的,又是人可阅读的,这将是一个很大的好处。

一些最新的开发工具既能根据你的Web service生成WSDL文档,又能导入WSDL文档,生成调用相应WebService的代理类代码。

2、CXF

CXF,apache 下的 WebService 的开源框架。

Apache CXF = Celtix + Xfire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF了,以下简称为 CXF。
Apache CXF 是一个开源的 web Services 框架,CXF 帮助您构建和开发 web Services ,它支持多种协议,
比如:SOAP1.1,1,2、XML/HTTP、REST HTTP 或者 CORBA。

灵活的部署:可以运行有 Tomcat,Jboss,weblogic,Jetty(内置)上面。

2.1 CXF入门Demo

(1)需求分析

我们这里是实现一个简单的天气预报的案例。
注意,我们不是做真正的天气预报,而是一个假的信息。传递城市名称,如果是北京返回雾霾,否则返回晴。
我们开发需要开发服务端客户端。

(2)服务端开发
1)创建maven工程,名称为cxfserver,打包方式为war

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2)pom.xml添加依赖以及相关插件

在这里插入图片描述

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.itzheng</groupId>
	<artifactId>cxfServer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<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-beans</artifactId>
			<version>4.2.4.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.2.4.RELEASE</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<!-- 指定端口 -->
					<port>9090</port>
					<!-- 请求路径 -->
					<path>/cxf</path>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
3)编写服务接口和服务类

IWeatherService

package com.itzheng.cxfserver;
import javax.jws.WebService;
/*
 * 天气服务接口
 */
@WebService
public interface IWeatherService {
    
    
	//根据城市返回天气信息
	String info(String city);
}

WeatherService


public class WeatherService implements IWeatherService {
    
    

	@Override
	public String info(String city) {
    
    
		
		if("北京".equals(city)) {
    
    
			return "雾霾";
		}
		return "晴天";
	}

}

4)配置web.xml文件

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  
  	<!-- 配置加载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>
  
	<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>
</web-app>
5)编写applicationContext-cxf.xml文件

在这里插入图片描述

<?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">
	<!-- WS调用的类 -->
	<bean id="weatherService"
		class="com.itzheng.cxfserver.impl.WeatherService">
	</bean>
	<!-- 发布服务 -->
	<jaxws:server address="/weatherService">
		<jaxws:serviceBean>
			<ref bean="weatherService" />
		</jaxws:serviceBean>
	</jaxws:server>

</beans>

(3)运行项目访问:
访问一:http://localhost:9090/cxf/ws/weatherService?wsdl

在这里插入图片描述

访问二:http://localhost:9090/cxf/ws/weatherService?wsdl=IWeatherService.wsdl

运行显示结果
在这里插入图片描述
解析上述返回信息:从下往上看
在这里插入图片描述

(4)客户端开发
1)创建Maven工程(jar)cxfclient

在这里插入图片描述
查看项目文件路径
在这里插入图片描述

2)根据 WSDL 生成本地代码

在这里插入图片描述

3) 开启cmd:命令行进入项目目录src/main/java目录下:

在这里插入图片描述
返回工程当中并刷新工程
在这里插入图片描述
自动生成一些类和方法
在这里插入图片描述

4)cxfclient/pox.xml当中引入依赖
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.itzheng</groupId>
	<artifactId>cxfServer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<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.11</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
5)编写 spring(cxf)配置文件applicationContext_cxf.xml

在这里插入图片描述

<?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.itzheng.cxfserver.impl.IWeatherService" >
	</jaxws:client>
</beans>
6)创建测试类,在测试目录下

在这里插入图片描述

package com.itzheng.cxf.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.itzheng.cxfserver.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("深圳"));
	}
}
7)运行结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_44757034/article/details/111869428