javascript
SpringBoot2 整合 XFIRE 服务端和客户端
文章目錄
- 一、集成XFIRE
- 1. 版本選型
- 2. 導入依賴
- 3. 注入XFireSpringServlet
- 4. 創建一個xml文件
- 5. 使用@ImportResource注入xml
- 6. 創建@WebService接口
- 6. 創建實現類
- 7. 添加配置類
- 8. 工具類
- 二、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沖突,需要將其排除依賴
<!--xfire start --><dependency><groupId>org.codehaus.xfire</groupId><artifactId>xfire-all</artifactId><version>1.2.6</version><!--排除沖突的依賴包--><exclusions><exclusion><groupId>javax.activation</groupId><artifactId>activation</artifactId></exclusion><exclusion><groupId>org.springframework</groupId><artifactId>spring</artifactId></exclusion><exclusion><artifactId>commons-logging</artifactId><groupId>commons-logging</groupId></exclusion><exclusion><artifactId>stax-api</artifactId><groupId>stax</groupId></exclusion></exclusions></dependency><!--axis end -->3. 注入XFireSpringServlet
注入XFireSpringServlet
創建XfireBootServlet類
相當于web.xml中的:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><servlet><servlet-name>xfireServlet</servlet-name><servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class></servlet><servlet-mapping><servlet-name>xfireServlet</servlet-name><url-pattern>/webservice/*</url-pattern></servlet-mapping> </web-app>4. 創建一個xml文件
在resources下創建cofnig文件夾,并在config文件夾下面創建boot-xfire.xml
5. 使用@ImportResource注入xml
package com.gblfy.ws.config;import org.springframework.context.annotation.ImportResource; import org.springframework.stereotype.Component;/*** 引入boot-xfire.xml配置文件** @author gblfy* @date 2021-09-17*/ @ImportResource(locations = {"classpath:config/boot-xfire.xml"}) @Component public class XfireConfig { }6. 創建@WebService接口
package com.gblfy.ws.service;import javax.jws.WebService;/*** Xfire接口** @author gblfy* @date 2021-09-17*/ @WebService public interface IXfireService {public String sayHello(String info);public String sayHello2(String info,String info2); }6. 創建實現類
package com.gblfy.ws.service.impl;import com.gblfy.ws.service.IXfireService; import com.gblfy.ws.utils.SpringBootBeanAutowiringSupport; import org.springframework.stereotype.Service;import javax.jws.WebService; import javax.xml.ws.BindingType; import javax.xml.ws.soap.SOAPBinding; /*** Xfire接口實現類** @author gblfy* @date 2021-09-17*//*** serviceName:?wsdl前綴* targetNamespace:命名空間* name:無實際意義*/ @WebService(serviceName = "xfireServiceShell", name = "xfireService",targetNamespace = "http://impl.service.ws.gblfy.com") @BindingType(value = SOAPBinding.SOAP12HTTP_BINDING) @Service //繼承SpringBootBeanAutowiringSupport 可以讓@Autowired注入成功,我重寫了WebApplicationContextLocator的onStartup方法 public class XfireServiceImpl extends SpringBootBeanAutowiringSupport implements IXfireService {/*** @param info* @return*/@Overridepublic String sayHello(String info) {return "sayHello:" + info;}@Overridepublic String sayHello2(String info, String info2) {return info + info2;} }7. 添加配置類
WebApplicationContextLocator
package com.gblfy.ws.config;import org.springframework.boot.web.servlet.ServletContextInitializer; import org.springframework.context.annotation.Configuration; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils;import javax.servlet.ServletContext; import javax.servlet.ServletException;/*** 手動獲取bean** @author gblfy* @date 2021-09-17*/ @Configuration public class WebApplicationContextLocator implements ServletContextInitializer {private static WebApplicationContext webApplicationContext;public static WebApplicationContext getCurrentWebApplicationContext() {return webApplicationContext;}/*** 在啟動時將servletContext 獲取出來,后面再讀取二次使用。* @param servletContext* @throws ServletException*/@Overridepublic void onStartup(ServletContext servletContext) throws ServletException {webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);} }8. 工具類
SpringBootBeanAutowiringSupport
package com.gblfy.ws.utils;import com.gblfy.ws.config.WebApplicationContextLocator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.web.context.WebApplicationContext;/*** 手動獲取bean** @author gblfy* @date 2021-09-17*/ public abstract class SpringBootBeanAutowiringSupport {private static final Logger logger = LoggerFactory.getLogger(SpringBootBeanAutowiringSupport.class);/*** This constructor performs injection on this instance,* based on the current web application context.* <p>Intended for use as a base class.** @see #processInjectionBasedOnCurrentContext*/public SpringBootBeanAutowiringSupport() {System.out.println("SpringBootBeanAutowiringSupport.SpringBootBeanAutowiringSupport");processInjectionBasedOnCurrentContext(this);}/*** Process {@code @Autowired} injection for the given target object,* based on the current web application context.* <p>Intended for use as a delegate.** @param target the target object to process* @see org.springframework.web.context.ContextLoader#getCurrentWebApplicationContext()*/public static void processInjectionBasedOnCurrentContext(Object target) {Assert.notNull(target, "Target object must not be null");WebApplicationContext cc = WebApplicationContextLocator.getCurrentWebApplicationContext();if (cc != null) {AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();bpp.setBeanFactory(cc.getAutowireCapableBeanFactory());bpp.processInjection(target);} else {if (logger.isDebugEnabled()) {logger.debug("Current WebApplicationContext is not available for processing of " +ClassUtils.getShortName(target.getClass()) + ": " +"Make sure this class gets constructed in a Spring web application. Proceeding without injection.");}}} }這樣就完成了!!!
二、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客戶端
package com.gblfy.ws.client;import org.codehaus.xfire.client.Client; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component;import java.net.URL;@Component public class XFireClient {private final static Logger log = LoggerFactory.getLogger(XFireClient.class);public static void main(String[] args) throws Exception {String xfireUrl = "http://localhost:8080/xfire/xfireServiceShell?wsdl";String namespaceURI = "http://impl.service.ws.gblfy.com";//單個參數String method = "sayHello";//多參// String method = "sayHello2";String reqXml = "1";String reqXml2 = "2";//調用服務XFireClient.xfireSendMsg(xfireUrl, namespaceURI, method, reqXml);// XFireClient.xfireSendMsg(xfireUrl, namespaceURI, method, reqXml, reqXml2);}/*** 單參調用工具類** @param xfireUrl url地址* @param method 調用方法名* @param reqXml 發送報文體* @return res 返回結果* @throws Exception 若有異常,在控制臺輸出異常,并將異常拋出*/public static String xfireSendMsg(String xfireUrl, String namespaceURI, String method, String reqXml) throws Exception {// 創建服務Client client = new Client(new URL(xfireUrl));// 設置調用的方法和方法的命名空間client.setProperty(namespaceURI, method);// 通過映射獲得結果Object[] result = new Object[0];try {result = client.invoke(method, new Object[]{reqXml});} catch (Exception e) {e.printStackTrace();throw e;}String xml = (String) result[0];log.info("響應報文 : {}", xml);return xml;}/*** 多參調用工具類(Object類型)** @param xfireUrl url地址* @param method 調用方法名* @param reqXml 發送報文體* @return res 返回結果* @throws Exception 若有異常,在控制臺輸出異常,并將異常拋出*/public static String xfireSendMsg(String xfireUrl, String namespaceURI, String method, String reqXml, String reqXml2) throws Exception {// 創建服務Client client = new Client(new URL(xfireUrl));// 設置調用的方法和方法的命名空間client.setProperty(namespaceURI, method);// 通過映射獲得結果Object[] result = new Object[0];try {result = client.invoke(method, new Object[]{reqXml, reqXml2});} catch (Exception e) {e.printStackTrace();throw e;}String xml = (String) result[0];log.info("響應報文 : {}", xml);return xml;} }開源源碼.
https://gitee.com/gb_90/unified-access-center
總結
以上是生活随笔為你收集整理的SpringBoot2 整合 XFIRE 服务端和客户端的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RuoYi-Cloud 部署篇_03(l
- 下一篇: Seata 的AT模式需求实战_04