SpringBoot2 整合 XFIRE 服务端和客户端

冷不防 2022-09-11 14:18 427阅读 0赞

在这里插入图片描述

文章目录

          • 一、集成XFIRE
              1. 版本选型
              1. 导入依赖
              1. 注入XFireSpringServlet
              1. 创建一个xml文件
              1. 使用@ImportResource注入xml
              1. 创建@WebService接口
              1. 创建实现类
              1. 添加配置类
              1. 工具类
          • 二、XFIRE 发布服务
            • 2.1. 运行项目
            • 2.2. 异常解决
            • 2.3. 测试验证
          • 三、XFIRE客户端
            • 开源源码.
一、集成XFIRE
1. 版本选型

















阿健/框架 版本
spring-boot 2.5.4
xfire-all 1.2.6
2. 导入依赖

导入xfire的依赖包xfire-all,会自动导入相关依赖包,其中spring可能会与项目本身的spring冲突,需要将其排除依赖

  1. <!--xfire start -->
  2. <dependency>
  3. <groupId>org.codehaus.xfire</groupId>
  4. <artifactId>xfire-all</artifactId>
  5. <version>1.2.6</version>
  6. <!--排除冲突的依赖包-->
  7. <exclusions>
  8. <exclusion>
  9. <groupId>javax.activation</groupId>
  10. <artifactId>activation</artifactId>
  11. </exclusion>
  12. <exclusion>
  13. <groupId>org.springframework</groupId>
  14. <artifactId>spring</artifactId>
  15. </exclusion>
  16. <exclusion>
  17. <artifactId>commons-logging</artifactId>
  18. <groupId>commons-logging</groupId>
  19. </exclusion>
  20. <exclusion>
  21. <artifactId>stax-api</artifactId>
  22. <groupId>stax</groupId>
  23. </exclusion>
  24. </exclusions>
  25. </dependency>
  26. <!--axis end -->
3. 注入XFireSpringServlet

注入XFireSpringServlet
创建XfireBootServlet类

  1. package com.gblfy.ws.servlet;
  2. import org.codehaus.xfire.spring.XFireSpringServlet;
  3. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. /**
  7. * @author gblfy
  8. * @date 2021-09-17
  9. */
  10. @Configuration
  11. public class XfireBootServlet {
  12. @Bean
  13. public ServletRegistrationBean registrationBean() {
  14. System.out.println("servletRegistrationBean----------");
  15. ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
  16. servletRegistrationBean.setServlet(new XFireSpringServlet());
  17. servletRegistrationBean.addUrlMappings("/webservice/*");
  18. return servletRegistrationBean;
  19. }
  20. }

相当于web.xml中的:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  4. <servlet>
  5. <servlet-name>xfireServlet</servlet-name>
  6. <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
  7. </servlet>
  8. <servlet-mapping>
  9. <servlet-name>xfireServlet</servlet-name>
  10. <url-pattern>/webservice/*</url-pattern>
  11. </servlet-mapping>
  12. </web-app>
4. 创建一个xml文件

在resources下创建cofnig文件夹,并在config文件夹下面创建boot-xfire.xml
在这里插入图片描述

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
  6. <!--扫描被@webService的包-->
  7. <context:component-scan base-package="com.gblfy.ws.service.impl"/>
  8. <!-- XFire start -->
  9. <import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>
  10. <!--<import resource="xfire.xml" />-->
  11. <bean id="webAnnotations" class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations"/>
  12. <bean id="jsr181HandlerMapping" class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping">
  13. <property name="xfire" ref="xfire"/>
  14. <property name="webAnnotations" ref="webAnnotations"/>
  15. </bean>
  16. </beans>
5. 使用@ImportResource注入xml
  1. package com.gblfy.ws.config;
  2. import org.springframework.context.annotation.ImportResource;
  3. import org.springframework.stereotype.Component;
  4. /**
  5. * 引入boot-xfire.xml配置文件
  6. *
  7. * @author gblfy
  8. * @date 2021-09-17
  9. */
  10. @ImportResource(locations = { "classpath:config/boot-xfire.xml"})
  11. @Component
  12. public class XfireConfig {
  13. }
6. 创建@WebService接口
  1. package com.gblfy.ws.service;
  2. import javax.jws.WebService;
  3. /**
  4. * Xfire接口
  5. *
  6. * @author gblfy
  7. * @date 2021-09-17
  8. */
  9. @WebService
  10. public interface IXfireService {
  11. public String sayHello(String info);
  12. public String sayHello2(String info,String info2);
  13. }
6. 创建实现类
  1. package com.gblfy.ws.service.impl;
  2. import com.gblfy.ws.service.IXfireService;
  3. import com.gblfy.ws.utils.SpringBootBeanAutowiringSupport;
  4. import org.springframework.stereotype.Service;
  5. import javax.jws.WebService;
  6. import javax.xml.ws.BindingType;
  7. import javax.xml.ws.soap.SOAPBinding;
  8. /**
  9. * Xfire接口实现类
  10. *
  11. * @author gblfy
  12. * @date 2021-09-17
  13. */
  14. /**
  15. * serviceName:?wsdl前缀
  16. * targetNamespace:命名空间
  17. * name:无实际意义
  18. */
  19. @WebService(serviceName = "xfireServiceShell", name = "xfireService",
  20. targetNamespace = "http://impl.service.ws.gblfy.com")
  21. @BindingType(value = SOAPBinding.SOAP12HTTP_BINDING)
  22. @Service
  23. //继承SpringBootBeanAutowiringSupport 可以让@Autowired注入成功,我重写了WebApplicationContextLocator的onStartup方法
  24. public class XfireServiceImpl extends SpringBootBeanAutowiringSupport implements IXfireService {
  25. /**
  26. * @param info
  27. * @return
  28. */
  29. @Override
  30. public String sayHello(String info) {
  31. return "sayHello:" + info;
  32. }
  33. @Override
  34. public String sayHello2(String info, String info2) {
  35. return info + info2;
  36. }
  37. }
7. 添加配置类

WebApplicationContextLocator

  1. package com.gblfy.ws.config;
  2. import org.springframework.boot.web.servlet.ServletContextInitializer;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.context.WebApplicationContext;
  5. import org.springframework.web.context.support.WebApplicationContextUtils;
  6. import javax.servlet.ServletContext;
  7. import javax.servlet.ServletException;
  8. /**
  9. * 手动获取bean
  10. *
  11. * @author gblfy
  12. * @date 2021-09-17
  13. */
  14. @Configuration
  15. public class WebApplicationContextLocator implements ServletContextInitializer {
  16. private static WebApplicationContext webApplicationContext;
  17. public static WebApplicationContext getCurrentWebApplicationContext() {
  18. return webApplicationContext;
  19. }
  20. /**
  21. * 在启动时将servletContext 获取出来,后面再读取二次使用。
  22. * @param servletContext
  23. * @throws ServletException
  24. */
  25. @Override
  26. public void onStartup(ServletContext servletContext) throws ServletException {
  27. webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
  28. }
  29. }
8. 工具类

SpringBootBeanAutowiringSupport

  1. package com.gblfy.ws.utils;
  2. import com.gblfy.ws.config.WebApplicationContextLocator;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
  6. import org.springframework.util.Assert;
  7. import org.springframework.util.ClassUtils;
  8. import org.springframework.web.context.WebApplicationContext;
  9. /**
  10. * 手动获取bean
  11. *
  12. * @author gblfy
  13. * @date 2021-09-17
  14. */
  15. public abstract class SpringBootBeanAutowiringSupport {
  16. private static final Logger logger = LoggerFactory.getLogger(SpringBootBeanAutowiringSupport.class);
  17. /**
  18. * This constructor performs injection on this instance,
  19. * based on the current web application context.
  20. * <p>Intended for use as a base class.
  21. *
  22. * @see #processInjectionBasedOnCurrentContext
  23. */
  24. public SpringBootBeanAutowiringSupport() {
  25. System.out.println("SpringBootBeanAutowiringSupport.SpringBootBeanAutowiringSupport");
  26. processInjectionBasedOnCurrentContext(this);
  27. }
  28. /**
  29. * Process { @code @Autowired} injection for the given target object,
  30. * based on the current web application context.
  31. * <p>Intended for use as a delegate.
  32. *
  33. * @param target the target object to process
  34. * @see org.springframework.web.context.ContextLoader#getCurrentWebApplicationContext()
  35. */
  36. public static void processInjectionBasedOnCurrentContext(Object target) {
  37. Assert.notNull(target, "Target object must not be null");
  38. WebApplicationContext cc = WebApplicationContextLocator.getCurrentWebApplicationContext();
  39. if (cc != null) {
  40. AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
  41. bpp.setBeanFactory(cc.getAutowireCapableBeanFactory());
  42. bpp.processInjection(target);
  43. } else {
  44. if (logger.isDebugEnabled()) {
  45. logger.debug("Current WebApplicationContext is not available for processing of " +
  46. ClassUtils.getShortName(target.getClass()) + ": " +
  47. "Make sure this class gets constructed in a Spring web application. Proceeding without injection.");
  48. }
  49. }
  50. }
  51. }

这样就完成了!!!

二、XFIRE 发布服务
2.1. 运行项目

在这里插入图片描述

2.2. 异常解决

但是在启动的时候遇到Attribute “singleton” must be declared for element type “bean”.
在这里插入图片描述
移步跳转,即可解决:
Attribute “singleton” must be declared for element type “bean”.

移步跳转,即可解决:
遇到cannot convert value of type ‘org.codehaus.xfire.spring.editors.ServiceFactoryEditor’
cannot convert value of type ‘org.codehaus.xfire.spring.editors.ServiceFactoryEditor

2.3. 测试验证

http://localhost:8080//webservice/xfireServiceShell?wsdl
在这里插入图片描述

三、XFIRE客户端
  1. package com.gblfy.ws.client;
  2. import org.codehaus.xfire.client.Client;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.stereotype.Component;
  6. import java.net.URL;
  7. @Component
  8. public class XFireClient {
  9. private final static Logger log = LoggerFactory.getLogger(XFireClient.class);
  10. public static void main(String[] args) throws Exception {
  11. String xfireUrl = "http://localhost:8080/xfire/xfireServiceShell?wsdl";
  12. String namespaceURI = "http://impl.service.ws.gblfy.com";
  13. //单个参数
  14. String method = "sayHello";
  15. //多参
  16. // String method = "sayHello2";
  17. String reqXml = "1";
  18. String reqXml2 = "2";
  19. //调用服务
  20. XFireClient.xfireSendMsg(xfireUrl, namespaceURI, method, reqXml);
  21. // XFireClient.xfireSendMsg(xfireUrl, namespaceURI, method, reqXml, reqXml2);
  22. }
  23. /**
  24. * 单参调用工具类
  25. *
  26. * @param xfireUrl url地址
  27. * @param method 调用方法名
  28. * @param reqXml 发送报文体
  29. * @return res 返回结果
  30. * @throws Exception 若有异常,在控制台输出异常,并将异常抛出
  31. */
  32. public static String xfireSendMsg(String xfireUrl, String namespaceURI, String method, String reqXml) throws Exception {
  33. // 创建服务
  34. Client client = new Client(new URL(xfireUrl));
  35. // 设置调用的方法和方法的命名空间
  36. client.setProperty(namespaceURI, method);
  37. // 通过映射获得结果
  38. Object[] result = new Object[0];
  39. try {
  40. result = client.invoke(method, new Object[]{ reqXml});
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. throw e;
  44. }
  45. String xml = (String) result[0];
  46. log.info("响应报文 : {}", xml);
  47. return xml;
  48. }
  49. /**
  50. * 多参调用工具类(Object类型)
  51. *
  52. * @param xfireUrl url地址
  53. * @param method 调用方法名
  54. * @param reqXml 发送报文体
  55. * @return res 返回结果
  56. * @throws Exception 若有异常,在控制台输出异常,并将异常抛出
  57. */
  58. public static String xfireSendMsg(String xfireUrl, String namespaceURI, String method, String reqXml, String reqXml2) throws Exception {
  59. // 创建服务
  60. Client client = new Client(new URL(xfireUrl));
  61. // 设置调用的方法和方法的命名空间
  62. client.setProperty(namespaceURI, method);
  63. // 通过映射获得结果
  64. Object[] result = new Object[0];
  65. try {
  66. result = client.invoke(method, new Object[]{ reqXml, reqXml2});
  67. } catch (Exception e) {
  68. e.printStackTrace();
  69. throw e;
  70. }
  71. String xml = (String) result[0];
  72. log.info("响应报文 : {}", xml);
  73. return xml;
  74. }
  75. }
开源源码.

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

发表评论

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

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

相关阅读