关键词:
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 区别:
- RS基于RESTful风格,WS基于soap的xml协议
- 传输数据:ws比rs效率低,rs不需要传递正文
- Ws只能向服务端传递xml,rs可传多种格式(e.g. xml/json)
JAXWS开发:
SpringBoot整合CXF-JAXWS 服务端
- 创建一个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>
- 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);
- 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);
- 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;
- Springboot启动类
@SpringBootApplication
public class CXFSpringApplication
public static void main(String[] args)
// TODO Auto-generated method stub
SpringApplication.run(CXFSpringApplication.class, args);
SpringBoot整合CXF-JAXWS 客户端
- 创建一个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>
- 接口(和服务端一样)
- 调用服务端接口
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 服务端
- 创建一个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>
- 接口
@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);
- 接口实现
- 配置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>
- 配置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 客户端
- 创建一个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>
<artifactIdwebservice实现之cxf
WebService一座服务端与客户端进行通讯的桥梁,通过WebService可以处理数据量大,需要java后台服务端进行操作的业务需求。通过CXF(WebService的框架)发布接口,CXF可以很好的与Spring进行集成1、下载CXF版本的相关jar包,Spring的包也... 查看详情
cxf如何创建webservice客户端
用cxf开发的webService,现在我想调用webServicepublicstaticvoidmain(String[]args)throwsException//这个是用cxf客户端访问cxf部署的webservice服务//千万记住,访问cxf的webservice必须加上namespace,否则通不过//现在又另外一个问题,传递过去的参数服务... 查看详情
关于webservice(cxf)的一些理解
CXF是apache下开源的webservice框架,CXF依赖spring进行集成,支持soap1.1,soap1.2,XML/HTTP,RESTfulHTTP或者CORBA; 使用jaxws来实现SOAP1.1,1.2;1.通过编程的方法来实现CXF发布使用接口 ; A.使用了jaxwsSeverFactoryBean 这个对象来进行... 查看详情
springboot整合cxf发布和调用webservice
一.前言说起webservice最近几年restful大行其道,大有取代传统soapwebservice的趋势,但是一些特有或相对老旧的系统依然使用了传统的soapwebservice,例如银行、航空公司的机票查询接口等。本博客主要讲解得是springboot整合cxf发布webserv... 查看详情
webservice010——cxf的简单介绍
CXF发布soap1.1版本的协议添加依赖文件,这个文件夹下的所有文件发布soap1.2版本的协议 查看详情
使用cxf写webservice的简单实例
增加CXF依赖<dependency><groupId>org.apache.cxf</groupId><artifactId>apache-cxf</artifactId><version>${cxf.version}</version><type>pom</type></depen 查看详情
springboot整合cxf发布webservice和客户端的调用
SpringBoot整合cxf发布webService1.看看项目结构图 2.cxf的pom依赖1<dependency>2<groupId>org.apache.cxf</groupId>3<artifactId>cxf-spring-boot-starter-jaxws</artifactId>4<version> 查看详情
使用 cxf (JaxWsProxyFactoryBean) 测试摘要认证的 webservice 客户端
】使用cxf(JaxWsProxyFactoryBean)测试摘要认证的webservice客户端【英文标题】:webserviceclientusingcxf(JaxWsProxyFactoryBean)totestdigestauthentication【发布时间】:2013-09-2216:26:46【问题描述】:如何使用cxf(JaxWsProxyFactoryBean)将摘要身份验证类型设置... 查看详情
cxf入门
1WebService简单描述?一言以蔽之:WebService是一种跨编程语言和跨操作系统平台的远程调用技术。常用的webservice框架:cfx、jwx、XFire与Axis2,jwx也就是jax-ws,是java6提供的对webservice的一种实现。cxf框架则简化了服务发布过程。Axis与XFir... 查看详情
cxf2.7整合spring发布webservice
---------==========--服务端发布webservice-=============--------1.需要的jar包: 2.包结构 3.代码1.实体类packagecn.qlq.domain;publicclassUser{privateStringusername;privateStringpassword;publicStringg 查看详情
webservice—cxf整合spring实现接口发布和调用过程
一、CXF整合Spring实现接口发布发布过程如下:1、引入jar包(基于maven管理)<!--cxf--><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>2.7 查看详情
cxf实现webservice(代码片段)
1.jarcxf2.2.6.jar ......2.web.xml <?xmlversion="1.0"encoding="UTF-8"?><web-appversion="3.0"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi& 查看详情
webservice的cxf和spring整合发布
1、新建一个web项目2、导入cxf相应的jar包,并部署到项目中3、服务接口1packagecom.xiaostudy;23/**4*@desc服务器接口5*@authorxiaostudy6*7*/8publicinterfaceTest_service{910publicStringgetNumber(Stringnumber);1112}4、服务接口实现类1packagecom.xiaos 查看详情
6.用cxf编写基于spring的webservice
首先是服务器端://实体类publicclassWeather{privateStringregion;//区域编码privateStringregionName;//区域名称privatefloattemperature;//温度privatebooleanisRain;//是否下雨publicWeather(){super();}publicWeather(Stringregion,Strin 查看详情
基于cxf的restful风格的webservice实现
1、建立cxf-server.xml放在src根目录<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org 查看详情
如何确保来自 JMS 队列的消息被传递到外部 WebService (CXF)?
】如何确保来自JMS队列的消息被传递到外部WebService(CXF)?【英文标题】:HowtomakesurethatmessagefromJMSqueueisdeliveredtoexternalWebService(CXF)?【发布时间】:2012-02-1719:00:12【问题描述】:问题应该如何在MuleESB3.2中配置ActiveMQ和<flow>... 查看详情
springboot整合cxf发布webservice与调用
参考技术AWebService技术,能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件,就可相互交换数据或集成。依据WebService规范实施的应用之间,无论它们所使用的语言、平台或内部协议是什么,都可以相... 查看详情
认识cxf(webservice框架)(代码片段)
ApacheCXF=Celtix+Xfire支持多种协议:1)SOAP1.1,1.22)HTTP3)CORBA(CommonObjectRequestBrokerArchitecture公共对象请求代理体系结构,早期语言使用的WS。C,C++,C#)4)可以与Spring进行快速无缝的整合5)灵活的部署:可以运行有Tomcat,Jboss,Jet... 查看详情