2021-05-14

Posted 涂涂努力ing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021-05-14相关的知识,希望对你有一定的参考价值。

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</groupId>
			<artifactId>spring-test</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
		</dependency>
	</dependencies>
	
	<!-- Tomcat插件 -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<port>8080</port>
					<server>tomcat7</server>
				</configuration>
			</plugin>
		</plugins>
	</build> 
</project>

  1. 接口
@WebService
public interface UserService {
	
	public List<User> create();
	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. 接口实现
  2. 配置web.xml
    目的:配置cxf框架servlet;指导访问applicationContext文件发布接口
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="
            http://java.sun.com/xml/ns/javaee
            http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
	<display-name>03.cfx-ws-spring-server</display-name>
	<!-- 监听器启动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>
		<servlet-name>CXFServlet</servlet-name>
	<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
		<!-- 在项目加载的时候创建 -->
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>

</web-app>
  1. 配置applicationContext.xml
<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:jaxws="http://cxf.apache.org/jaxws"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
	
	<!-- 发布接口 -->
	<jaxws:server address="/userService" serviceClass="service.UserService">
		<jaxws:serviceBean>
			<bean class="impl.UserServiceImpl"></bean>
		</jaxws:serviceBean>
		<jaxws:inInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
		</jaxws:inInterceptors>
		<jaxws:outInterceptors>
			<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
		</jaxws:outInterceptors>
	</jaxws:server>
	</beans>

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</groupId>
			<artifactId>spring-test</artifactId>
			<version>4.2.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId2021-05-14要保持团队作战的模式

变量常量作用域 2021-05-14

Mculover666的博客文章导航(嵌入式宝藏站)(2021.05.14更新)

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

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

2021-05-14 Redis面试题 Redis的内存淘汰策略有哪些?