SpringBoot2 整合 CXF 服务端和客户端

以你之姓@ 2022-09-11 14:24 382阅读 0赞

在这里插入图片描述

文章目录

          • 一、CXF服务端
              1. 导入依赖
              1. 创建service接口
              1. 接口实现类
              1. cxf配置类
              1. 查看wsdl结果
          • 二、CXF客户端
            • 2.1. 客户端
            • 2.2. 断点调试
            • 2.3. 发起调用服务
            • 开源源码.
一、CXF服务端
1. 导入依赖
  1. <properties>
  2. <cxf.version>3.3.1</cxf.version>
  3. </properties>
  4. <!-- CXF webservice -->
  5. <dependency>
  6. <groupId>org.apache.cxf</groupId>
  7. <artifactId>cxf-rt-frontend-jaxws</artifactId>
  8. <version>${cxf.version}</version>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.apache.cxf</groupId>
  12. <artifactId>cxf-rt-transports-http</artifactId>
  13. <version>${cxf.version}</version>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.apache.cxf</groupId>
  17. <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
  18. <version>${cxf.version}</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.hibernate</groupId>
  22. <artifactId>hibernate-validator</artifactId>
  23. <version>5.2.4.Final</version>
  24. </dependency>
2. 创建service接口





























注解 简介
@WebService 放在接口上用于标记为webService服务接口
targetNamespace 命名空间
name 服务接口的名字,可不写
@WebMethod 标记为webService服务的方法
@WebParam 标记为webService服务的方法入参
  1. package com.gblfy.ws.service;
  2. import javax.jws.WebMethod;
  3. import javax.jws.WebParam;
  4. import javax.jws.WebService;
  5. /**
  6. * cxf接口
  7. *
  8. * @author gblfy
  9. * @date 2021-09-17
  10. */
  11. @WebService(targetNamespace = "http://service.ws.gblfy.com/", name = "ICxfService")
  12. public interface ICxfService {
  13. @WebMethod
  14. public String sayhello(@WebParam(name = "request") String request);
  15. }
3. 接口实现类
  1. package com.gblfy.ws.service.impl;
  2. import com.gblfy.ws.service.ICxfService;
  3. import org.springframework.stereotype.Component;
  4. import javax.jws.WebService;
  5. /**
  6. * cxf接口实现类
  7. *
  8. * @author gblfy
  9. * @date 2021-09-17
  10. */
  11. @WebService(serviceName = "cxfServiceShell",//对外发布的服务名
  12. targetNamespace = "http://service.ws.gblfy.com/",//指定你想要的名称空间,通常使用使用包名反转
  13. endpointInterface = "com.gblfy.ws.service.ICxfService")
  14. @Component
  15. public class CxfServiceImpl implements ICxfService {
  16. @Override
  17. public String sayhello(String request) {
  18. return "gblfy.com " + request;
  19. }
  20. }
4. cxf配置类
  1. package com.gblfy.ws.config;
  2. import com.gblfy.ws.service.ICxfService;
  3. import com.gblfy.ws.service.impl.CxfServiceImpl;
  4. import org.apache.cxf.Bus;
  5. import org.apache.cxf.bus.spring.SpringBus;
  6. import org.apache.cxf.jaxws.EndpointImpl;
  7. import org.apache.cxf.transport.servlet.CXFServlet;
  8. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. @Configuration
  12. public class CxfConfig {
  13. @Bean
  14. public ServletRegistrationBean cxfServlet() {
  15. return new ServletRegistrationBean(new CXFServlet(), "/cxf/*");
  16. }
  17. @Bean(name = Bus.DEFAULT_BUS_ID)
  18. public SpringBus springBus() {
  19. return new SpringBus();
  20. }
  21. @Bean
  22. public ICxfService cxfService() {
  23. return new CxfServiceImpl();
  24. }
  25. /**
  26. * 发布服务并指定访问URL
  27. *
  28. * @return
  29. */
  30. @Bean
  31. public EndpointImpl userEnpoint() {
  32. EndpointImpl endpoint = new EndpointImpl(springBus(), cxfService());
  33. endpoint.publish("/cxfServiceShell");
  34. return endpoint;
  35. }
  36. }
5. 查看wsdl结果

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

二、CXF客户端
2.1. 客户端
  1. package com.gblfy.ws.client;
  2. import org.apache.cxf.endpoint.Client;
  3. import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
  4. import org.springframework.stereotype.Component;
  5. /**
  6. * cxf客户端调用(企业内部已封装)
  7. *
  8. * @author gblfy
  9. * @date 2021-09-17
  10. */
  11. @Component
  12. public class CxfClient {
  13. public static void main(String[] args) throws Exception {
  14. String cxfUrl = "http://127.0.0.1:8080/cxf/cxfServiceShell?wsdl";
  15. String method = "sayhello";
  16. String reqXml = "1";
  17. //调用服务
  18. CxfClient.cxfClientSingleParam(cxfUrl, method, reqXml);
  19. }
  20. /**
  21. * 单/多参调用工具类(Object类型)
  22. *
  23. * @param cxfUrl url地址
  24. * @param method 调用方法名
  25. * @param reqXml 发送报文体
  26. * @return res 返回结果
  27. * @throws Exception 若有异常,在控制台输出异常,并将异常抛出
  28. */
  29. public static String cxfClientSingleParam(String cxfUrl, String method, Object... reqXml) throws Exception {
  30. String res = null;
  31. // 创建动态客户端
  32. JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
  33. Client client = dcf.createClient(cxfUrl);
  34. // 需要密码的情况需要加上用户名和密码
  35. // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
  36. Object[] objects = new Object[0];
  37. try {
  38. // 基本格式:invoke("方法名",参数1,参数2,参数3....);
  39. objects = client.invoke(method, reqXml);
  40. res = objects[0].toString();
  41. System.out.println("返回数据:" + res);
  42. } catch (java.lang.Exception e) {
  43. e.printStackTrace();
  44. throw e;
  45. }
  46. return res;
  47. }
  48. }
2.2. 断点调试

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

2.3. 发起调用服务

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

开源源码.

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

发表评论

表情:
评论列表 (有 0 条评论,382人围观)

还没有评论,来说两句吧...

相关阅读