日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot2 整合 AXIS 服务端和客户端

發布時間:2024/9/27 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot2 整合 AXIS 服务端和客户端 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


文章目錄

          • 一、服務端
            • 1. 版本選型
            • 2.導入依賴
            • 3. SERVLET
            • 4. 接口
            • 5.實現類
            • 6. 配置工廠
            • 7.啟動類
            • 8. WEB-INF目錄1
            • 8. WEB-INF目錄2
            • 9. /目錄1
            • 9. /目錄2
            • 10. wsdd
            • 11. 測試驗證
          • 二、客戶端
            • 開源源碼.

一、服務端
1. 版本選型
阿健/框架版本
spring-boot2.5.4
axis1.4
axis-jaxrpc1.4
commons-discovery0.2
wsdl4j1.6.3
2.導入依賴
<!--axis start --><dependency><groupId>org.apache.axis</groupId><artifactId>axis</artifactId><version>1.4</version></dependency><dependency><groupId>axis</groupId><artifactId>axis-jaxrpc</artifactId><version>1.4</version></dependency><dependency><groupId>commons-discovery</groupId><artifactId>commons-discovery</artifactId><version>0.2</version></dependency><dependency><groupId>wsdl4j</groupId><artifactId>wsdl4j</artifactId><version>1.6.3</version></dependency><!--axis end -->
3. SERVLET
package com.gblfy.ws.servlet;import org.apache.axis.transport.http.AxisServlet;/*** AxisServlet** @author gblfy* @date 2021-09-17*/ @javax.servlet.annotation.WebServlet(urlPatterns = "/services/*",loadOnStartup = 1,name = "AxisServlet" ) public class AxisBootServlet extends AxisServlet { }
4. 接口
package com.gblfy.ws.service;/*** Axis接口** @author gblfy* @date 2021-09-17*/ public interface IAxisService {public String sayHello(String info);public String sayHello2(String info,String info2); }
5.實現類
package com.gblfy.ws.service.impl;import com.gblfy.ws.service.IAxisService; import org.springframework.stereotype.Service;/*** Axis接口實現類** @author gblfy* @date 2021-09-17*/ @Service public class AxisServiceImpl implements IAxisService {/*** @param info* @return*/@Overridepublic String sayHello(String info) {return "sayHello:" + info;}@Overridepublic String sayHello2(String info, String info2) {return info + info2;} }
6. 配置工廠

新建org.apache.axis.configuration包
在新建的包下新建EngineConfigurationFactoryServlet類,集成EngineConfigurationFactoryDefault

第一種:目錄為
String appWebInfPath = “/WEB-INF”;

第二種:目錄為
String appWebInfPath = “/”;

package org.apache.axis.configuration;/** Copyright 2002-2004 The Apache Software Foundation.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/ import org.apache.axis.AxisProperties; import org.apache.axis.ConfigurationException; import org.apache.axis.EngineConfiguration; import org.apache.axis.EngineConfigurationFactory; import org.apache.axis.components.logger.LogFactory; import org.apache.axis.server.AxisServer; import org.apache.axis.utils.ClassUtils; import org.apache.axis.utils.Messages; import org.apache.commons.logging.Log;import javax.servlet.ServletConfig; import java.io.InputStream;/*** This is a default implementation of ServletEngineConfigurationFactory.* It is user-overrideable by a system property without affecting* the caller. If you decide to override it, use delegation if* you want to inherit the behaviour of this class as using* class extension will result in tight loops. That is, your* class should implement EngineConfigurationFactory and keep* an instance of this class in a member field and delegate* methods to that instance when the default behaviour is* required.** @author Richard A. Sitze* @author Davanum Srinivas (dims@apache.org)*/ public class EngineConfigurationFactoryServletextends EngineConfigurationFactoryDefault {protected static Log log =LogFactory.getLog(EngineConfigurationFactoryServlet.class.getName());private ServletConfig cfg;/*** Creates and returns a new EngineConfigurationFactory.* If a factory cannot be created, return 'null'.* <p>* The factory may return non-NULL only if:* - it knows what to do with the param (param instanceof ServletContext)* - it can find it's configuration information** @see EngineConfigurationFactoryFinder*/public static EngineConfigurationFactory newFactory(Object param) {/*** Default, let this one go through if we find a ServletContext.** The REAL reason we are not trying to make any* decision here is because it's impossible* (without refactoring FileProvider) to determine* if a *.wsdd file is present or not until the configuration* is bound to an engine.** FileProvider/EngineConfiguration pretend to be independent,* but they are tightly bound to an engine instance...*/return (param instanceof ServletConfig)? new EngineConfigurationFactoryServlet((ServletConfig) param): null;}/*** Create the default engine configuration and detect whether the user* has overridden this with their own.*/protected EngineConfigurationFactoryServlet(ServletConfig conf) {super();this.cfg = conf;}/*** Get a default server engine configuration.** @return a server EngineConfiguration*/public EngineConfiguration getServerEngineConfig() {return getServerEngineConfig(cfg);}/*** Get a default server engine configuration in a servlet environment.* <p>* //* @param ctx a ServletContext** @return a server EngineConfiguration*/private static EngineConfiguration getServerEngineConfig(ServletConfig cfg) {String configFile = cfg.getInitParameter(OPTION_SERVER_CONFIG_FILE);if (configFile == null)configFile =AxisProperties.getProperty(OPTION_SERVER_CONFIG_FILE);if (configFile == null) {configFile = SERVER_CONFIG_FILE;}// String appWebInfPath = "/";String appWebInfPath = "/WEB-INF";//由于部署方式變更為jar部署,此處不可以使用改方式獲取路徑// ServletContext ctx = cfg.getServletContext();// String realWebInfPath = ctx.getRealPath(appWebInfPath);FileProvider config = null;String realWebInfPath = EngineConfigurationFactoryServlet.class.getResource(appWebInfPath).getPath();InputStream iss = ClassUtils.getResourceAsStream(EngineConfigurationFactoryServlet.class, appWebInfPath + "/" + SERVER_CONFIG_FILE);if (iss != null) {// FileProvider assumes responsibility for 'is':// do NOT call is.close().config = new FileProvider(iss);}if (config == null) {log.error(Messages.getMessage("servletEngineWebInfError03", ""));}/*** Couldn't get data OR file does exist.* If we have a path, then attempt to either open* the existing file, or create an (empty) file.*/if (config == null && realWebInfPath != null) {try {config = new FileProvider(realWebInfPath, configFile);} catch (ConfigurationException e) {log.error(Messages.getMessage("servletEngineWebInfError00"), e);}}/*** Fall back to config file packaged with AxisEngine*/if (config == null) {log.warn(Messages.getMessage("servletEngineWebInfWarn00"));try {InputStream is =ClassUtils.getResourceAsStream(AxisServer.class,SERVER_CONFIG_FILE);config = new FileProvider(is);} catch (Exception e) {log.error(Messages.getMessage("servletEngineWebInfError02"), e);}}return config;}}
7.啟動類

@ServletComponentScan //掃描自定義的WebFilter和WebListener,否則無法掃描到
8. WEB-INF目錄1

如果上面工廠的靜態目錄選擇 WEB-INF,請認真閱讀這一小節,如果選擇/則可以跳過這一小節,靜態目錄選擇 WEB-INF有以下二種場景:
第一種:在main目錄下,創建webapp/WEB-INF目錄
調整目錄屬性

8. WEB-INF目錄2

第二種:在resources目錄下面WEB-INF目錄

9. /目錄1

如果 6. 配置工廠選擇的靜態目錄為/,請認真閱讀這一小節,有以下二種場景:
第一種:在resources下面存放server-config.wsdd文件

9. /目錄2

第二種:在webapp下面存放server-config.wsdd文件
,若果選擇在webapp下面存放server-config.wsdd文件,請調整目錄屬性

10. wsdd

根據上面選擇的場景,在指定的目錄下面創建server-config.wsdd文件,內容如下:

  • 第一種:在webapp的WEB-INF目錄下面

  • 第二種:在webapp目錄下面

  • 第三種:在resources的WEB-INF目錄下面

  • 第四種:在resources目錄下面

<?xml version="1.0" encoding="UTF-8"?> <deployment xmlns="http://xml.apache.org/axis/wsdd/"xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"><handler type="java:org.apache.axis.handlers.http.URLMapper"name="URLMapper"/><!--要告訴別人的接口名--><service name="AxisServiceShell" provider="java:RPC"><!--這個是 實現類--><parameter name="className" value="com.gblfy.ws.service.impl.AxisServiceImpl"/><!--命名空間設置:默認:http://+ip:端口+urlPatterns+name(暴露的服務名)例如:http://localhost:8080/services/AxisServiceShell自定義格式:<namespace>自定義命名空間</namespace>例如:<namespace>com.gblfy.ws.service.impl</namespace>--><!--這是是暴露的方法名 比如可以值暴露一個--><parameter name="allowedMethods" value="sayHello"/><!--這是是暴露的方法名 也可以用* 表示暴露全部的public方法--><!--<parameter name="allowedMethods" value="*" />--></service><transport name="http"><requestFlow><handler type="URLMapper"/></requestFlow></transport></deployment>
11. 測試驗證

http://localhost:8080/services/AxisServiceShell?wsdl

二、客戶端
package com.gblfy.ws.client;import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.util.ObjectUtils;import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import java.rmi.RemoteException;/*** Axis客戶端** @author guobin* @date 2021-09-17*/ @Component public class AxisClient {private final static Logger log = LoggerFactory.getLogger(AxisClient.class);public static void main(String[] args) throws Exception {String axisUrl = "http://localhost:8080/services/axisServiceShell?wsdl";String namespaceURI = "http://localhost:8080/services/axisServiceShell";// String method = "sayHello";String method = "sayHello2";String reqXml = "1";String reqXml2 = "2";//調用axis服務// AxisClient.axisSendMsg(axisUrl, namespaceURI, method, reqXml);AxisClient.axisSendMsg(axisUrl, namespaceURI, method, reqXml, reqXml2);}/*** @param url WebService地址* @param namespace 命名空間* @param method 方法* @param tReqXml 請求報文* @return* @throws Exception*/public static String axisSendMsg(String url, String namespace, String method, String tReqXml)throws Exception {Service service = new Service();Call call;String res = null;call = (Call) service.createCall();long forStrTime = 0L;if (StringUtils.isBlank(url)) {throw new RuntimeException("調用url地址等于空,請核實地址是否正確!");}if (StringUtils.isBlank(namespace)) {throw new RuntimeException("調用namespace等于空,請核實命名空間是否正確!");}if (StringUtils.isBlank(method)) {throw new RuntimeException("調用method等于空,請核實方法名是否正確!");}if (ObjectUtils.isEmpty(tReqXml)) {throw new RuntimeException("調發送報文等于空,請核實發送內容是否正確!");}call.setTargetEndpointAddress(new java.net.URL(url));//特殊處理部分 startString subUrl = url.substring(url.lastIndexOf("/"));log.info("轉發路徑標志 {}", subUrl.substring(1, subUrl.length()));//針對xxx需求添加單獨邏輯判斷if ("ws_fxhx_ws".equals(subUrl.substring(1, subUrl.length()))) {call.addParameter("xmlStr", org.apache.axis.Constants.XSD_STRING, ParameterMode.IN);call.setReturnType(org.apache.axis.Constants.XSD_STRING);}//特殊處理部分 endcall.setOperationName(new QName(namespace, method));// 這是要調用的方法log.info("開始轉發請求報文");forStrTime = System.currentTimeMillis();log.info("開始轉發時間: {}-毫秒", forStrTime);try {res = (String) call.invoke(new Object[]{tReqXml});} catch (RemoteException e) {e.printStackTrace();throw e;}long forEndTime = System.currentTimeMillis();log.info("轉發結束時間: {}-毫秒", forEndTime);long endToStart = (long) (forEndTime - forStrTime);log.info("轉發消耗的時間:: {}-毫秒", endToStart);log.info("響應報文: {}", res);return res;}public static String axisSendMsg(String url, String namespace, String method, String tReqXml, String tReqXml2)throws Exception {Service service = new Service();Call call;String res = null;call = (Call) service.createCall();long forStrTime = 0L;if (StringUtils.isBlank(url)) {throw new RuntimeException("調用url地址等于空,請核實地址是否正確!");}if (StringUtils.isBlank(namespace)) {throw new RuntimeException("調用namespace等于空,請核實命名空間是否正確!");}if (StringUtils.isBlank(method)) {throw new RuntimeException("調用method等于空,請核實方法名是否正確!");}if (ObjectUtils.isEmpty(tReqXml)) {throw new RuntimeException("調發送報文等于空,請核實發送內容是否正確!");}call.setTargetEndpointAddress(new java.net.URL(url));//特殊處理部分 startString subUrl = url.substring(url.lastIndexOf("/"));log.info("轉發路徑標志 {}", subUrl.substring(1, subUrl.length()));//針對xxx需求添加單獨邏輯判斷if ("ws_fxhx_ws".equals(subUrl.substring(1, subUrl.length()))) {call.addParameter("xmlStr", org.apache.axis.Constants.XSD_STRING, ParameterMode.IN);call.setReturnType(org.apache.axis.Constants.XSD_STRING);}//特殊處理部分 endcall.setOperationName(new QName(namespace, method));// 這是要調用的方法log.info("開始轉發請求報文");forStrTime = System.currentTimeMillis();log.info("開始轉發時間: {}-毫秒", forStrTime);try {res = (String) call.invoke(new Object[]{tReqXml, tReqXml2});} catch (RemoteException e) {e.printStackTrace();throw e;}long forEndTime = System.currentTimeMillis();log.info("轉發結束時間: {}-毫秒", forEndTime);long endToStart = (long) (forEndTime - forStrTime);log.info("轉發消耗的時間:: {}-毫秒", endToStart);log.info("響應報文: {}", res);return res;} }
開源源碼.

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

總結

以上是生活随笔為你收集整理的SpringBoot2 整合 AXIS 服务端和客户端的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。