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 ...
随机推荐
- 关于JAVA的一些零碎小知识
1.经常遇到集合之间需要互相转化的 Array和List转化:Arrays.asList(数组):list.toArray(); List和Set转化:Set<String> set = ...
- redis入门指南(七)—— 安全、协议、管理工具及命令属性
写在前面 学习<redis入门指南>笔记,结合实践,只记录重要,明确,属于新知的相关内容. 安全 1.可以使用bind参数绑定一个地址,使redis只接受这个地址的连接. 2.使用requ ...
- Git裸仓库的分支(Active Branch)切换
Git裸仓库的Active Branch切换方法 在服务器上通过init --bare创建了一个裸仓库作为远程仓库使用,并且存在三个分支(master/kid/develop),但在使用中发现代码虽然 ...
- [De1CTF 2019]Giftbox 分析&&TPOP学习
[De1CTF 2019]Giftbox 刚进来我以为是直接给了shell,恐怖如斯. 随便扔了个命令,之后就没然后了,hhh,截包发现可能存在sql注入. 然后我就不会了... what i lea ...
- spring-boot-route(一)Controller接收参数的几种方式
Controller接收参数的常用方式总体可以分为三类.第一类是Get请求通过拼接url进行传递,第二类是Post请求通过请求体进行传递,第三类是通过请求头部进行参数传递. 1 @PathVariab ...
- 基于springboot工程浅谈整合rabbitmq怎么样防止消息发送mq不丢失和消费mq的消息防止丢失
本文只针对springboot整合rabbitmq的消息防丢失,话不多说,上干货.... 设置发送mq消息不丢失实现思路 执行的方案: 第一步,要对队列,消息以及交换机进行持久化操作(保存到物理磁盘中 ...
- Ubuntu通过Apache安装WebDav
使用KeePass保存密码,在个人服务器上安装WebDav协议. # 安装Apache2服务器 sudo aptitude install -y apache2 # 开启Apache2中对WebDav ...
- Windows10系统下wsappx占用CPU资源过高?wsappx是什么?如何关闭wsappx进程?
在Windows10系统开机的时候,wsappx进程占用的CPU资源非常高,导致电脑运行速度缓慢,那么我们如何关闭wsappx进程,让电脑加快运行速度呢?下面就一起来看一下操作的方法吧. [现象] 1 ...
- visio调整画布大小的简便方法
按住Ctrl,然后鼠标在边缘拉拽即可.
- 使用git 版本控制的代码在线修调试,如何还原
在线调试: 先切换成www用户进入项目的根目录比如/data/wwwroot/website su www cd /data/wwwroot/website vi ./api/controllers/ ...