javascript
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-boot | 2.5.4 |
| axis | 1.4 |
| axis-jaxrpc | 1.4 |
| commons-discovery | 0.2 |
| wsdl4j | 1.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 = “/”;
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目錄下面
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 服务端和客户端的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue vant Area组件使用详解
- 下一篇: RuoYi-Vue 部署 Linux环境