SpringBoot2 整合 CXF 服务端和客户端

在这里插入图片描述

一、CXF服务端
1. 导入依赖
   <properties>
        <cxf.version>3.3.1</cxf.version>
    </properties>
  
     <!-- CXF webservice -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>
           <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.4.Final</version>
        </dependency>
2. 创建service接口
注解简介
@WebService放在接口上用于标记为webService服务接口
targetNamespace命名空间
name服务接口的名字,可不写
@WebMethod标记为webService服务的方法
@WebParam标记为webService服务的方法入参
package com.gblfy.ws.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 * cxf接口
 *
 * @author gblfy
 * @date 2021-09-17
 */
@WebService(targetNamespace = "http://service.ws.gblfy.com/", name = "ICxfService")
public interface ICxfService {

    @WebMethod
    public String sayhello(@WebParam(name = "request") String request);
}

3. 接口实现类
package com.gblfy.ws.service.impl;

import com.gblfy.ws.service.ICxfService;
import org.springframework.stereotype.Component;

import javax.jws.WebService;

/**
 * cxf接口实现类
 *
 * @author gblfy
 * @date 2021-09-17
 */
@WebService(serviceName = "cxfServiceShell",//对外发布的服务名
        targetNamespace = "http://service.ws.gblfy.com/",//指定你想要的名称空间,通常使用使用包名反转
        endpointInterface = "com.gblfy.ws.service.ICxfService")
@Component
public class CxfServiceImpl implements ICxfService {

    @Override
    public String sayhello(String request) {
        return "gblfy.com " + request;
    }
}


4. cxf配置类
package com.gblfy.ws.config;

import com.gblfy.ws.service.ICxfService;
import com.gblfy.ws.service.impl.CxfServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CxfConfig {
    
    @Bean
    public ServletRegistrationBean cxfServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/cxf/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public ICxfService cxfService() {
        return new CxfServiceImpl();
    }

    /**
     * 发布服务并指定访问URL
     *
     * @return
     */
    @Bean
    public EndpointImpl userEnpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), cxfService());
        endpoint.publish("/cxfServiceShell");
        return endpoint;
    }
}

5. 查看wsdl结果

(1)配置启动端口 server.port: 8080
(2)启动springBoot启动类 输入 localhost:8080/cxf 可以看到自己发布的服务
http://localhost:8080/cxf/cxfServiceShell?wsdl
在这里插入图片描述

二、CXF客户端
2.1. 客户端
package com.gblfy.ws.client;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.stereotype.Component;

/**
 * cxf客户端调用(企业内部已封装)
 *
 * @author gblfy
 * @date 2021-09-17
 */
@Component
public class CxfClient {
    public static void main(String[] args) throws Exception {
        String cxfUrl = "http://127.0.0.1:8080/cxf/cxfServiceShell?wsdl";
        String method = "sayhello";
        String reqXml = "1";

        //调用服务
        CxfClient.cxfClientSingleParam(cxfUrl, method, reqXml);
    }

    /**
     * 单/多参调用工具类(Object类型)
     *
     * @param cxfUrl url地址
     * @param method 调用方法名
     * @param reqXml 发送报文体
     * @return res 返回结果
     * @throws Exception 若有异常,在控制台输出异常,并将异常抛出
     */
    public static String cxfClientSingleParam(String cxfUrl, String method, Object... reqXml) throws Exception {
        String res = null;
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(cxfUrl);

        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // 基本格式:invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke(method, reqXml);
            res = objects[0].toString();
            System.out.println("返回数据:" + res);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
            throw e;
        }
        return res;
    }
}
2.2. 断点调试

在这里插入图片描述
在这里插入图片描述

2.3. 发起调用服务

在这里插入图片描述
在这里插入图片描述

开源源码.

https://gitee.com/gb_90/unified-access-center

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论
### 回答1: Spring Boot是一个开发框架,它提供了很多便利的功能来简化Spring应用程序的开发。CXF是一个用于构建和部署Web服务的开源框架。整合Spring Boot和CXF可以让我们更容易地开发和部署Web服务。 要在Spring Boot中整合CXF,我们需要首先添加CXF的依赖。可以在pom.xml文件中添加以下代码来引入CXF的依赖: ```xml <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.3.1</version> </dependency> ``` 接下来,我们需要创建一个配置类来配置CXF。可以创建一个名为CxfConfig的类,并使用@Configuration注解进行标注。在该类中,我们可以配置CXF的一些属性,例如请求路径和服务实现类等。 ```java @Configuration public class CxfConfig { @Autowired private Bus bus; @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(bus, new YourServiceImpl()); endpoint.publish("/your-service"); return endpoint; } } ``` 在上面的配置类中,我们使用@Autowired注解将Spring Boot的默认总线注入进来,并且创建了一个名为endpoint的bean。通过EndpointImpl类,我们可以将我们的服务实现类(YourServiceImpl)和请求路径(/your-service)与CXF进行绑定。 最后,我们需要在主应用类上添加@EnableWebServices注解,使得Spring Boot能够自动注册和部署我们的Web服务。 ```java @SpringBootApplication @EnableWebServices public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 通过以上步骤,我们就完成了Spring Boot和CXF整合。现在我们可以启动应用程序,并通过访问相应的路径来调用我们的Web服务了。 总结起来,Spring Boot整合CXF可以让我们更方便地开发和部署Web服务。我们只需要添加相关的依赖和配置类,并使用@EnableWebServices注解来注册和部署服务即可。整合CXF后,我们可以更加高效地构建和处理Web服务。 ### 回答2: Spring Boot是一个开源的Java开发框架,可以快速构建独立的、可运行的、生产级别的基于Spring框架的应用程序。而Apache CXF是一个Web服务框架,可以用于构建和开发SOAP和RESTful风格的Web服务。 在Spring Boot中整合CXF可以实现基于Java的Web服务的开发和部署。首先,我们需要引入CXF的依赖项,可以在pom.xml文件中添加相关的依赖。然后,我们可以创建一个配置类,使用CXF提供的注解配置Web服务的相关信息,例如服务地址、端口等。在配置类中,可以使用@Endpoint注解来标注Web服务的具体实现类。接下来,我们可以创建一个Spring Boot启动类,使用@EnableAutoConfiguration注解启用Spring Boot的自动配置功能,并使用@ImportResource注解导入CXF的配置文件。 通过以上步骤,我们就可以在Spring Boot应用中集成CXF。当应用启动时,CXF将根据配置类和注解,自动发布Web服务,并根据接口和实现类自动生成相关的WSDL文件和SOAP消息。此时,我们可以使用SOAPUI等工具来测试和调用这些Web服务。另外,CXF还提供了一些扩展点和配置选项,可以用于定制和优化Web服务的性能和安全性。 总的来说,Spring Boot整合CXF可以帮助我们快速、高效地开发和部署基于Java的Web服务。通过使用Spring Boot的自动配置功能和CXF的注解配置,我们可以简化Web服务的开发过程,并提供可靠和高性能的Web服务。同时,Spring Boot还支持其他的Web服务框架,例如Jersey、Axis等,可以根据实际需求选择合适的框架。 ### 回答3: Spring Boot是一个开发框架,可以帮助开发者快速搭建基于Spring的Java应用程序。而Apache CXF是一个开源的Web服务框架,可以用来构建和部署SOAP和RESTful服务。 在Spring Boot中整合CXF可以使用以下步骤: 1.添加依赖:首先,在项目的pom.xml文件中添加CXF的依赖,以便能够使用CXF的相关功能。 2.配置CXF的Web服务:在Spring Boot的配置文件中,添加CXF的相关配置,包括服务地址、请求拦截器等。 3.创建Web服务接口:在项目中创建Web服务接口,定义服务的方法、参数和返回值。 4.实现Web服务接口:实现接口,并在实现类中编写具体的业务逻辑。 5.发布Web服务:使用CXF的相关注解,将实现类发布为Web服务,使其能够被外部访问。 6.启动应用程序:启动Spring Boot应用程序,让CXF的Web服务开始监听请求。 通过以上步骤,我们就可以使用Spring Boot来整合CXF,从而搭建一个可以提供SOAP或RESTful服务的应用程序。在这个应用程序中,我们可以使用CXF的一些特性,如拦截器和异常处理等,来增加应用程序的功能和稳定性。 总而言之,Spring Boot整合CXF能够大大简化CXF的配置过程,并使开发者能够更快地构建和部署Web服务。通过这种方式,我们可以更加高效地开发和管理我们的应用程序,提供更好的用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gblfy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值