一、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实践的更多相关文章

  1. springboot整合apache ftpserver详细教程(看这一篇就够了)

    原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/12192425.html,否则将追究法律责任!!! 一.Apache ftpserver相关 ...

  2. Spring 3 整合Apache CXF WebService[转]

    http://www.cnblogs.com/hoojo/archive/2012/07/13/2590593.html 在CXF2版本中,整合Spring3发布CXF WebService就更加简单 ...

  3. springboot 整合apache shiro

    这几天因为项目需要,学习了下shiro,由此留下一些记录,也希望对初学shiro的朋友有帮助. springboot 是这两年新兴起来的一个项目,它的出现是为了减少springmvc开发过程中需要引入 ...

  4. SpringBoot整合Apache Shiro权限验证框架

    比较常见的权限框架有两种,一种是Spring Security,另一种是Apache Shiro,两种框架各有优劣,个人感觉Shiro更容易使用,更加灵活,也更符合RABC规则,而且是java官方更推 ...

  5. SpringBoot整合Redis初实践

    Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理. 有时,为了提升整个网站的性能,在开发时会将经常访问的数据进行缓存,这样在调用这个数据接口时,可以提 ...

  6. SpringBoot整合Apache Shiro

    Subject 用户主体 (把操作交给SecurityManager)SecurityManager 安全管理器 (关联Realm)Realm   Shiro连接数据的桥梁 引入maven依赖 < ...

  7. springboot 整合 CXF 版本异常 java.lang.NoClassDefFoundError:ServletRegistrationBean

    在使用SpringBoot 项目整合webservice组件 CXF的时候,在启动时,抛出异常如下,查阅资料初步判断为版本问题.升级到高版本后正常启动. cxf 刚开始使用版本  3.1.7 后更新为 ...

  8. Springboot整合cxf后不能访问controller,不能访问接口

    参考版本 springboot 1.4.X <=========> cxf-spring-boot-starter-jaxws 3.1.X springboot 1.5.X <=== ...

  9. 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 ...

随机推荐

  1. 智慧出行--maas

    未来智慧出行新生态——MaaS系统的解读与畅想 -城市交通:观察与思考 - 未来智慧出行新生态——MaaS系统的解读与畅想 在货运领域,有一种承运方式叫“多式联运”,它是由承运人与货主签订一份货运合同 ...

  2. java执行器

    Executor 执行已提交的 Runnable 任务对象.此接口提供一种将任务提交与每个任务将如何运行的机制(包括线程使用的细节.调度等)分离开来的方法.Executor 接口并没有严格地要求执行是 ...

  3. 简单说说Restful API

    前言: 最近一段时间,一直在低头敲代码,开发平台对外交互的API接口,功能已经大体完成了,回过头来看看自己的接口设计文档,不胜感慨,想当初自己也是为"接口名称"想破了脑袋,各种百度 ...

  4. JavaScript的this到底代表谁?(this指向哪里?)

    在很多编程语言中都有this这个特殊关键字的存在,比如Java中的this,还有本文要说到的JavaScript中的this.那么,JavaScript中this究竟有什么特性和用法呢?它又是如何定义 ...

  5. Java递归算法经典实例(兔子问题、阶乘、1到100累加)

    https://blog.csdn.net/isitman/article/details/61199070

  6. Vue 组件的基础介绍

    1.组件定义 1.定义组件并引用 2.父组件向子组件传值 3.子组件向父组件传值 # 组件间传值:vuex (https://www.cnblogs.com/xiaonq/p/9697921.html ...

  7. 01 学习人工智能,不做笔记?做笔记不知道如何输入数学公式?“onenote+Mathematics Add-In”拯救你!onenote安装数学输入公式插件Microsoft Mathematics Add-In for Word and OneNote教程走一波

    一.Microsoft Mathematics Add-In 插件下载 Microsoft Mathematics Add-In for Word and OneNote插件下载链接: https:/ ...

  8. HTML & CSS & JavaScript 从一个表格到一个灰阶颜色表 01

    工具1:HBuilder X 1.9.9.20190522 工具2:火狐浏览器 67.0.4 (64 位) 在开始之前,你需要了解一些基本的知识,不要求你能掌握,但是了解一下还是有益的: HTML 简 ...

  9. NOIP提高组2013 D2T3 【华容道】

    某王  老师给我们考了一场noip2013的真题...心态爆炸! 题目大意: 有一个n*m的棋盘,每个格子上都有一个棋子,有些格子上的棋子能够移动(可移动的棋子是固定的),棋盘中有一个格子是空的,仍何 ...

  10. 在Windows7中打开照片,提示“Windows 照片查看器无法显示此图片,因为计算机上的可用内存可能不足。....”

    在Windows7中打开照片,提示"Windows 照片查看器无法显示此图片,因为计算机上的可用内存可能不足.请关闭一些目前没有使用的程序或者释放部分硬盘空间(如果硬盘几乎已满),然后重试. ...