2021-05-14 WebService简介 CXF框架及其两种编程方式 JAVA

WebService简介 CXF框架及其两种编程方式 JAVA

本文是我第一次接触webservice的学习成果,着重学习了CXF框架,以及JAXWS和JARWS两种模式整合了Spring和Springboot的运用方式,总结了框架的逻辑流程和易错点
本文项目内容及源码内容基于https://edu.51cto.com/course/13645.html这门课程

简介

WebService 是一种低耦合的应用程序
基于客户端和服务端的远程调用技术
应用场景
用于异构系统的整合(使用不同编程语言的系统)
Apache CXF
CXF = Celtix + XFire
CXF实质是一种框架,支持2种编程方式:JAXWS 和 JAXRS
JAXWS — Java Api for XML WebService
JAXRS — Java Api for RESTful WebService

JAXWS底层原理:

在这里插入图片描述

远程调用的本质是信息的传递而不是内存调用
JAXRS:
RESTful: 软件架构设计风格,不是标准(非强制性);用于客户端与服务端交互
RESTful风格特点: 请求地址为同一个,但用不同的请求方法则效果不同(尽可能用不同的请求方式来请求)
JAXWS 和 JAXRS 区别:

  1. RS基于RESTful风格,WS基于soap的xml协议
  2. 传输数据:ws比rs效率低,rs不需要传递正文
  3. Ws只能向服务端传递xml,rs可传多种格式(e.g. xml/json)

JAXWS开发:

SpringBoot整合CXF-JAXWS 服务端

  1. 创建一个maven程序,导入pom
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>cxf</groupId>
	<artifactId>08.cxf-ws-springboot-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-parent</artifactId>
			<version>1.5.4.RELEASE</version>
			<type>pom</type>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
			<version>3.1.11</version>
		</dependency>
	</dependencies>
	<properties>
		<java.version>1.8</java.version>
	</properties>
</project>
  1. Service接口
/**
 * @author Siyu Tu
 * @WebService 把该类标注为WebService接口,可能被远程调用
 * 该注解须在jdk 1.6以上
 */

 @WebService
public interface UserService {
   
	
	public void saveUser(User user);

	public void updateUser(User user);

	public void deleteUser(Integer id);
	
	public List<User> findAllUser();
	
	public User findById(Integer id);

}
  1. Service实现类
@Service
public class UserServiceImpl implements UserService {
   

	@Override
	public void saveUser(User user) {
   
		// TODO Auto-generated method stub
		System.out.println("保存用户: "+user);
		
	}

	@Override
	public void updateUser(User user) {
   
		// TODO Auto-generated method stub
		System.out.println("修改用户: "+user);
		
	}
  1. Config文件
@Configuration
public class JaxWsConfig {
   
	@Autowired
	private Bus bus; //发布中间件
	
	@Autowired
	private UserService userService;
	
	@Bean
	public Endpoint createEndpoint() {
   
		Endpoint endpoint = new EndpointImpl(bus,userService);
		endpoint.publish("/userService"); //发布地址
		return endpoint;
	}
}
  1. Springboot启动类
@SpringBootApplication
public class CXFSpringApplication {
   

	public static void main(String[] args) {
   
		// TODO Auto-generated method stub
		SpringApplication.run(CXFSpringApplication.class, args);
	}

}

SpringBoot整合CXF-JAXWS 客户端

  1. 创建一个Maven项目,导入pom
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com</groupId>
	<artifactId>09.cxf-ws-springboot-client</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<!-- cxf对jaws的支持 -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>3.0.1</version>
		</dependency>
		<!-- 内置jetty web服务器 -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>3.0.1</version>
		</dependency>

		<!-- 依赖日志 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.6.6</version>
		</dependency>
	</dependencies>
</project>
  1. 接口(和服务端一样)
  2. 调用服务端接口
public class ClientDemo {
   
	public static void main(String[] args) {
   
	// 1.创建工厂对象
	JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

	// 2.设置参数
	// 2.1 设置访问路径
factory.setAddress("http://localhost:8080/services/userService");

	// 2.2设置接口
factory.setServiceClass(UserService.class);
		
UserService userService = (UserService) factory.create();
			
Client client =ClientProxy.getClient(userService);
			//输入拦截器
			client.getInInterceptors().add(new LoggingInInterceptor());
			// 添加响应消息拦截器
			client.getOutInterceptors().add(new LoggingOutInterceptor());
			// 发布服务相当于JDK的 EndPoint.publish()
			// 调用服务端接口的方法

			List<User> users = userService.findAllUser();
			for(User u:users) System.out.println(u);
			
	}
}

Spring整合cxf-ws 服务端

  1. 创建一个maven项目,导入pom
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>cxf</groupId>
	<artifactId>03.cfx-ws-spring-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<dependencies>
		<!-- cxf对jaws的支持 -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>3.0.1</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework<
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值