SpringBoot整合Apache-CXF实践

SpringBoot整合Apache-CXF实践,第1张

概述一、Apache CXF是什么? Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。 一、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  BlogCxfServerApplication {    static voID main(String[] args) {        SpringApplication.run(BlogCxfServerApplication.,args);        System.out.println("====启动Blog Cxf Server====");    }}
b.编写application.yml配置文件
# Tomcatserver:  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# Springspring:  application:    # 应用名称    name: blog-cxf-servercxf:  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/)interface UserService {    *     * 添加用户     * @param email     * @param username     * @param password     * @return     */    int addUser(@WebParam(name = email") String email,@WebParam(name = username") String username,1)">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 = 指定你想要的名称空间,通常使用使用包名反转        endpointInterface = com.blog.cxf.server.service.UserService)@Component UserServiceImpl implements UserService {    int addUser(String email,String username,String password) {        System.注册用户:"+email);        return 1;    }     updateUser(UserReqDto userReqDto) {        ;    }}

数据传输类(UserReqDto.java):

package com.blog.cxf.server.dto;import lombok.Data;* * @description: * @author: youcong * @time: 2020/10/24 22:49 @Data UserReqDto {    private Long ID;     String email;     String username;     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 CxfConfig {    @Bean(name = Bus.DEFAulT_BUS_ID)    public SpringBus springBus() {        return new SpringBus();    }    @Bean     UserService userService() {         UserServiceImpl();    }    *     * 发布服务并指定访问URL     * @return         @Bean     EndpointImpl userEnpoint() {        EndpointImpl endpoint =  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>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  BlogCxfClIEntApplication {     main(String[] args) {        SpringApplication.run(BlogCxfClIEntApplication.====启动Blog Cxf ClIEnt====);    }}
(3)编写application.yml
9091# Springspring:  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( UserAPIController {    @PostMapPing(/add)     add(@RequestParam String email,@RequestParam String username,@RequestParam String password) {        try {             接口地址            String address = http://127.0.0.1:9090/cxf/user?wsdl;             代理工厂            JaxWsProxyfactorybean jaxWsProxyfactorybean =  JaxWsProxyfactorybean();             设置代理地址            jaxWsProxyfactorybean.setAddress(address);             设置接口类型            jaxWsProxyfactorybean.setServiceClass(UserService.);             创建一个代理接口实现            UserService userService = (UserService) jaxWsProxyfactorybean.create();             userService.addUser(email,username,password);        } catch (Exception e) {            e.printstacktrace();            return -;        }    }}

注意:
实际中这段代码应该放在blog-cxf-server里面的Controller,然后客户端通过http-clIEnt或者其它http工具包进行请求。

还有如果是服务是都在一起,可按照maven依赖导入的方式来实现两个不同项目进行调用。

(5)使用PostMan测试

接着服务端控制台会打印如下:

三、代码例子

代码例子已上传到我的GitHub上。

代码地址:
https://github.com/developers-youcong/blog-cxf

总结

以上是内存溢出为你收集整理的SpringBoot整合Apache-CXF实践全部内容,希望文章能够帮你解决SpringBoot整合Apache-CXF实践所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1217187.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存