SpringBoot整合Apache-CXF实践
一、Apache CXF是什么?
Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTP、JMS 或者 JBI,CXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然地和 Spring 进行无缝集成。
二、SpringBoot整合Apache CXF实践例子
本次例子为Client-Server(客户端-服务端)。还是以我最喜欢的Blog为例。
本次涉及两个项目,一个是blog-cxf-client,另外一个是blog-cxf-server。
1.blog-cxf-server
(1)导入Maven依赖
<dependencies>
<!-- SpringBoot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- CXF webservice -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.4</version>
</dependency>
<!-- Lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency> <dependency>
<artifactId>blog-cxf-server</artifactId>
<groupId>com.blog.cxf</groupId>
<version>1.0</version>
</dependency> </dependencies>
(2)编写相关代码
a.编写主类
package com.blog.cxf.server; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; /**
* @description:
* @author: youcong
* @time: 2020/10/24 22:30
*/
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class BlogCxfServerApplication { public static void main(String[] args) {
SpringApplication.run(BlogCxfServerApplication.class, args);
System.out.println("====启动Blog Cxf Server===="); }
}
b.编写application.yml配置文件
# Tomcat
server:
tomcat:
uri-encoding: UTF-8
#最小线程数
min-spare-threads: 500
#最大线程数
max-threads: 2500
#最大连接数
max-connections: 5000
#最大等待队列长度
accept-count: 1000
#请求头最大长度kb
max-http-header-size: 1048576
#启动APR(非阻塞IO)
protocol: org.apache.coyote.http11.Http11AprProtocol
port: 9090 # Spring
spring:
application:
# 应用名称
name: blog-cxf-server
cxf:
path: /cxf
c.编写service代码
UserService.java
package com.blog.cxf.server.service; import com.blog.cxf.server.dto.UserReqDto; import javax.jws.WebParam;
import javax.jws.WebService; /**
* @description:
* @author: youcong
* @time: 2020/10/24 22:32
*/
@WebService(targetNamespace = "http://service.server.cxf.blog.com/")
public interface UserService { /**
* 添加用户
* @param email
* @param username
* @param password
* @return
*/
int addUser(@WebParam(name = "email") String email, @WebParam(name = "username") String username, @WebParam(name = "password") String password); /**
* 更新用户信息
* @param userReqDto
* @return
*/
int updateUser(@WebParam(name="user")UserReqDto userReqDto);
}
UserServiceImpl.java
package com.blog.cxf.server.service.impl; import com.blog.cxf.server.dto.UserReqDto;
import com.blog.cxf.server.service.UserService;
import org.springframework.stereotype.Component; import javax.jws.WebService; /**
* @description:
* @author: youcong
* @time: 2020/10/24 22:35
*/
@WebService(serviceName = "userService",//对外发布的服务名
targetNamespace = "http://service.server.cxf.blog.com/",//指定你想要的名称空间,通常使用使用包名反转
endpointInterface = "com.blog.cxf.server.service.UserService")
@Component
public class UserServiceImpl implements UserService {
public int addUser(String email, String username, String password) {
System.out.println("注册用户:"+email);
return 1;
} public int updateUser(UserReqDto userReqDto) {
return 1;
}
}
数据传输类(UserReqDto.java):
package com.blog.cxf.server.dto; import lombok.Data; /**
* @description:
* @author: youcong
* @time: 2020/10/24 22:49
*/
@Data
public class UserReqDto { private Long ID; private String email; private String username; private String password;
}
c.编写配置类(服务发布)
package com.blog.cxf.server.config;
import com.blog.cxf.server.service.UserService;
import com.blog.cxf.server.service.impl.UserServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @description:
* @author: youcong
* @time: 2020/10/24 22:37
*/
@Configuration
public class CxfConfig { @Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
} @Bean
public UserService userService() {
return new UserServiceImpl();
} /**
* 发布服务并指定访问URL
* @return
*/
@Bean
public EndpointImpl userEnpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), userService());
endpoint.publish("/user");
return endpoint;
} }
(3)启动BlogCxfServerApplication主类并访问对应的WSDL
访问路径:
http://localhost:9090/cxf/user?wsdl
截图效果:
使用SOAP-UI工具进行测试:

2.blog-cxf-client
(1)导入Maven依赖
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>blog-cxf</artifactId>
<groupId>com.blog.cxf</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>blog-cxf-client</artifactId>
<dependencies>
<!-- SpringBoot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- CXF webservice -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.4</version>
</dependency>
<!-- Lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency> <dependency>
<artifactId>blog-cxf-server</artifactId>
<groupId>com.blog.cxf</groupId>
<version>1.0</version>
</dependency> </dependencies> </project>
(2)编写主类
package com.blog.cxf.client; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; /**
* @description:
* @author: youcong
* @time: 2020/10/24 23:35
*/
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class BlogCxfClientApplication { public static void main(String[] args) {
SpringApplication.run(BlogCxfClientApplication.class, args);
System.out.println("====启动Blog Cxf Client===="); }
}
(3)编写application.yml
# Tomcat
server:
tomcat:
uri-encoding: UTF-8
#最小线程数
min-spare-threads: 500
#最大线程数
max-threads: 2500
#最大连接数
max-connections: 5000
#最大等待队列长度
accept-count: 1000
#请求头最大长度kb
max-http-header-size: 1048576
#启动APR(非阻塞IO)
protocol: org.apache.coyote.http11.Http11AprProtocol
port: 9091 # Spring
spring:
application:
# 应用名称
name: blog-cxf-client
(4)编写Controller
package com.blog.cxf.client.controller; import com.blog.cxf.server.dto.UserReqDto;
import com.blog.cxf.server.service.UserService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.web.bind.annotation.*; /**
* @description:
* @author: youcong
* @time: 2020/10/24 23:37
*/
@RestController
@RequestMapping("/user")
public class UserApiController { @PostMapping("/add")
public int add(@RequestParam String email, @RequestParam String username, @RequestParam String password) { try {
// 接口地址
String address = "http://127.0.0.1:9090/cxf/user?wsdl";
// 代理工厂
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
// 设置代理地址
jaxWsProxyFactoryBean.setAddress(address);
// 设置接口类型
jaxWsProxyFactoryBean.setServiceClass(UserService.class);
// 创建一个代理接口实现
UserService userService = (UserService) jaxWsProxyFactoryBean.create(); return userService.addUser(email, username, password);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
}
注意:
实际中这段代码应该放在blog-cxf-server里面的Controller,然后客户端通过http-client或者其它http工具包进行请求。
还有如果是服务是都在一起,可按照maven依赖导入的方式来实现两个不同项目进行调用。
(5)使用PostMan测试

接着服务端控制台会打印如下:
三、代码例子
代码例子已上传到我的GitHub上。
代码地址:
https://github.com/developers-youcong/blog-cxf
SpringBoot整合Apache-CXF实践的更多相关文章
- springboot整合apache ftpserver详细教程(看这一篇就够了)
原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/12192425.html,否则将追究法律责任!!! 一.Apache ftpserver相关 ...
- Spring 3 整合Apache CXF WebService[转]
http://www.cnblogs.com/hoojo/archive/2012/07/13/2590593.html 在CXF2版本中,整合Spring3发布CXF WebService就更加简单 ...
- springboot 整合apache shiro
这几天因为项目需要,学习了下shiro,由此留下一些记录,也希望对初学shiro的朋友有帮助. springboot 是这两年新兴起来的一个项目,它的出现是为了减少springmvc开发过程中需要引入 ...
- SpringBoot整合Apache Shiro权限验证框架
比较常见的权限框架有两种,一种是Spring Security,另一种是Apache Shiro,两种框架各有优劣,个人感觉Shiro更容易使用,更加灵活,也更符合RABC规则,而且是java官方更推 ...
- SpringBoot整合Redis初实践
Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理. 有时,为了提升整个网站的性能,在开发时会将经常访问的数据进行缓存,这样在调用这个数据接口时,可以提 ...
- SpringBoot整合Apache Shiro
Subject 用户主体 (把操作交给SecurityManager)SecurityManager 安全管理器 (关联Realm)Realm Shiro连接数据的桥梁 引入maven依赖 < ...
- springboot 整合 CXF 版本异常 java.lang.NoClassDefFoundError:ServletRegistrationBean
在使用SpringBoot 项目整合webservice组件 CXF的时候,在启动时,抛出异常如下,查阅资料初步判断为版本问题.升级到高版本后正常启动. cxf 刚开始使用版本 3.1.7 后更新为 ...
- Springboot整合cxf后不能访问controller,不能访问接口
参考版本 springboot 1.4.X <=========> cxf-spring-boot-starter-jaxws 3.1.X springboot 1.5.X <=== ...
- SpringBoot整合Mybatis注解版---update出现org.apache.ibatis.binding.BindingException: Parameter 'XXX' not found. Available parameters are [arg1, arg0, param1, param2]
SpringBoot整合Mybatis注解版---update时出现的问题 问题描述: 1.sql建表语句 DROP TABLE IF EXISTS `department`; CREATE TABL ...
随机推荐
- hystrix文档翻译之运维
hystrix不仅用作工程可靠性还可以用来运维. 这里将会分享一个拥有100+Hystrix命令,40+线程池,每天有100亿次线程请求,2000亿次信号量请求的系统是如何使用hystrix运维的.这 ...
- SpringBoot-03-配置之yaml语法学习
3. SpringBoot配置 3.1 yaml语法学习 配置文件 Springboot使用一个全局的配置文件,配置文件名称固定 spplication.properties 语法结构:key=val ...
- VUE开发(一)Spring Boot整合Vue并实现前后端贯穿调用
文章更新时间:2020/03/14 一.前言 作为一个后端程序员,前端知识多少还是要了解一些的,vue能很好的实现前后端分离,且更便于我们日常中的调试,还具备了轻量.低侵入性的特点,所以我觉得是很有必 ...
- Spring学习(三)Spring AOP 简介
一.简介 定义 aop就是面向切面编程,在数据库事务中切面编程被广泛使用. 在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 核心业务:比如登陆,增加数据,删除数据都叫核心业务 周边功能 ...
- shiro-重写标签功能----shiro:hasPermission 标签重写
public abstract class ShiroAuthorizingRealm extends AuthorizingRealm{ private static final String OR ...
- 关于数学公式Markdown
打开写博客(这都打不开就...) 再打开"选项". 选"启用数学公式支持"对(以后有用嘻嘻) 在默认编辑器里有Markdown选对. 然后就可以 \[\sum\ ...
- dedecmsv5.7sp1远程文件包含漏洞审计
dedecms5.7 sp1版本存在远程文件包含漏洞,在此记录审计复现漏洞过程. 漏洞在/install/index.php(index.php.bak)文件中,漏洞起因是$$符号使用不当,导致变量覆 ...
- 02 ArcPython的使用大纲
一.什么情况下使用ArcPython? 1.现有工具实现不了,可以用python 2.流程化需要时,可以使用python 3.没有AE等二次开发环境 4.其他特殊场景 二.ArcPython在ArcG ...
- 洛谷比赛 「EZEC」 Round 4
洛谷比赛 「EZEC」 Round 4 T1 zrmpaul Loves Array 题目描述 小 Z 有一个下标从 \(1\) 开始并且长度为 \(n\) 的序列,初始时下标为 \(i\) 位置的数 ...
- Pock 把 Touch Bar 变成系统中的 Dock 栏
Pock 把 Touch Bar 变成系统中的 Dock 栏 Pock 是一款 macOS App,你可以通过它把 Touch Bar 变成系统中的 Dock 栏,直接用来切换和启动 App,尽享全屏 ...