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 ...
随机推荐
- spring boot之Thymeleaf
没怎么仔细研究,就是spring boot的服务器需要一个登录界面,用这个实现了一个白板式的页面. https://blog.csdn.net/yelllowcong/article/details/ ...
- redis命令和RedisTemplate操作对应表
redis命令和RedisTemplate操作对应表 redisTemplate.opsForValue();//操作字符串 redisTemplate.opsForHash();//操作hash r ...
- canvas的简单绘制及设置
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> ...
- Java基础一篇过(四)List这篇就够了
文章更新时间:2020/08/03 一.List介绍 list是Java的一个接口,继承了Collection,常用到的有3个子类实现: ArrayList 底层数据结构是数组.线程不安全 Linke ...
- 搜索引擎学习(三)Lucene查询索引
一.查询理论 创建查询:构建一个包含了文档域和语汇单元的文档查询对象.(例:fileName:lucene) 查询过程:根据查询对象的条件,在索引中找出相应的term,然后根据term找到对应的文档i ...
- 数据类型-字符串(str)
1.只要是被单引号,双引号,三引号括起来的,都是字符串类型 2.字符串里面元素:单个字母,单个符号,都称之为一个元素 例如:s='hello!' (6个元素) len(数据)统计数据的长度pri ...
- linux与linux间,互相拷贝文件
直接使用scp命令 和远程Linux主机 进行文件的拷贝 1.可以将远程Linux系统上的文件拷贝到本地计算机 2.也可以将本地计算机上的文件拷贝到远程Linux系统上. 比如:我们要拷贝 ...
- Centos-检查文件系统并尝试修复-fsck
fsck 检查文件系统并尝试修改错误,修复对象为设备,本质上是调用 /sbin/fsck.filesystemName 命令, filesystemName是指定设备的文件系统类型,如图分区中有文件丢 ...
- Opencv的imread用法
所有参考来自网上仅仅做学习记录用,具体正确性需要在具体项目各自验证,不涉及具体错误代码处理调试等问题,欢迎发现发现问题~ 参考: 1. https://blog.csdn.net/LiheZhu/ar ...
- Azure内容审查器之羞羞图审查
上一篇 Azure 内容审查器之文本审查我们已经介绍了如果使用Azure进行文字内容的审核.对于社区内容,上传的图片是否含有羞羞内容也是需要过虑的.但是最为一般开发者自己很难实现这种级别的智能识别.但 ...