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

歡迎訪問 生活随笔!

生活随笔

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

javascript

SpringBoot2 整合 AXIS2 服务端和客户端

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

文章目錄

          • 一、AXIS2服務端
            • 1. 版本選型
            • 2.導入依賴
            • 3. services.xml
            • 4.Axis2配置類
            • 5.服務接口
            • 6.服務接口實現類
            • 7. FileCopyUtils工具類
            • 8. 測試驗證
          • 二、AXIS2服務端
            • 2.1. 客戶端類
            • 2.2. 服務調用測試
            • 開源源碼.

一、AXIS2服務端
1. 版本選型
阿健/框架版本
spring-boot2.5.5
axis21.7.9
2.導入依賴
<properties><axis2.version>1.7.9</axis2.version></properties><!--axis2 begin --><dependency><groupId>org.apache.axis2</groupId><artifactId>axis2-adb</artifactId><version>${axis2.version}</version></dependency><dependency><groupId>org.apache.axis2</groupId><artifactId>axis2-kernel</artifactId><version>${axis2.version}</version></dependency><dependency><groupId>org.apache.axis2</groupId><artifactId>axis2-transport-http</artifactId><version>${axis2.version}</version><exclusions><exclusion><groupId>javax-servlet</groupId><artifactId>servlet-api</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.apache.axis2</groupId><artifactId>axis2-transport-local</artifactId><version>${axis2.version}</version></dependency><dependency><groupId>org.apache.axis2</groupId><artifactId>axis2-jaxws</artifactId><version>${axis2.version}</version></dependency><!--axis2 end -->
3. services.xml

首先創建基本路徑:WEB-INF/services/axis2/META-INF/services.xml
注:路徑名必須和上面一致

services.xml內容

<?xml version="1.0" encoding="UTF-8" ?> <serviceGroup><!--name:暴露的服務名 根據需求配置或者自定義scope:默認即可targetNamespace:命名空間 根據需求配置或者自定義--><service name="axis2Service" scope="application" targetNamespace="http://service.axis2.gblfy.com"><description>simple axis2 webservice example</description><!--命名空間 根據需求配置或者自定義--><schema schemaNamespace="http://service.axis2.gblfy.com"/><!--實現類全路徑--><parameter name="ServiceClass">com.gblfy.axis2.service.impl.Axis2ServiceImpl</parameter><operation name="sayHello"><messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/></operation></service> </serviceGroup>
4.Axis2配置類

Axis2WebServiceConfiguration是webservice最主要的配置類,主要作用為讀取services.xml配置文件,內容如下:

package com.gblfy.axis2.config;import com.gblfy.axis2.utils.FileCopyUtils; import org.apache.axis2.transport.http.AxisServlet; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;import java.io.IOException;/*** axis2配置類,用于設置AxisServlet和訪問讀取services.xml文件** @author gblfy* @date 2021-09-25*/ @Configuration public class Axis2WebServiceConfiguration {//服務訪問前綴public static final String URL_PATH = "/services/*";//services.xml文件的位置public static final String SERVICES_FILE_PATH = "WEB-INF/services/axis2/META-INF/services.xml";//AXIS2參數keypublic static final String AXIS2_REP_PATH = "axis2.repository.path";@Beanpublic ServletRegistrationBean axis2Servlet() {ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();servletRegistrationBean.setServlet(new AxisServlet());servletRegistrationBean.addUrlMappings(URL_PATH);// 通過默認路徑無法找到services.xml,這里需要指定一下路徑,且必須是絕對路徑String path = this.getClass().getResource("/WEB-INF").getPath().toString();if (path.toLowerCase().startsWith("file:")) {path = path.substring(5);}if (path.indexOf("!") != -1) {try {FileCopyUtils.copy(SERVICES_FILE_PATH);} catch (IOException e) {e.printStackTrace();}path = path.substring(0, path.lastIndexOf("/", path.indexOf("!"))) + "/WEB-INF";}//System.out.println("xml配置文件,path={ "+path+" }");servletRegistrationBean.addInitParameter(AXIS2_REP_PATH, path);servletRegistrationBean.setLoadOnStartup(1);return servletRegistrationBean;}}
5.服務接口
package com.gblfy.axis2.service;/*** axis2接口類** @author gblfy* @date 2021-09-25*/ public interface Axis2Service {public String sayHello(String name); }
6.服務接口實現類
package com.gblfy.axis2.service.impl;import com.gblfy.axis2.service.Axis2Service; import org.springframework.stereotype.Service;/*** axis2接口實現類** @author gblfy* @date 2021-09-25*/ @Service public class Axis2ServiceImpl implements Axis2Service {@Overridepublic String sayHello(String name) {return "hello, " + name;} }
7. FileCopyUtils工具類

FileCopyUtils上面Axis2WebServiceConfiguration配置類有用到,主要作用確保service.xml的調用,代碼如下:

package com.gblfy.axis2.utils;import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths;/*** 將jar內的文件復制到jar包外的同級目錄下** @author gblfy* @date 2021-09-25*/ public class FileCopyUtils {private static final Logger log = LoggerFactory.getLogger(FileCopyUtils.class);private static InputStream getResource(String location) throws IOException {InputStream in = null;try {PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();in = resolver.getResource(location).getInputStream();byte[] byteArray = IOUtils.toByteArray(in);return new ByteArrayInputStream(byteArray);} catch (Exception e) {e.printStackTrace();log.error("getResource is error: {}", e);return null;} finally {if (in != null) {in.close();}}}/*** 獲取項目所在文件夾的絕對路徑** @return*/private static String getCurrentDirPath() {URL url = FileCopyUtils.class.getProtectionDomain().getCodeSource().getLocation();String path = url.getPath();if (path.startsWith("file:")) {path = path.replace("file:", "");}if (path.contains(".jar!/")) {path = path.substring(0, path.indexOf(".jar!/") + 4);}File file = new File(path);path = file.getParentFile().getAbsolutePath();return path;}private static Path getDistFile(String path) throws IOException {String currentRealPath = getCurrentDirPath();Path dist = Paths.get(currentRealPath + File.separator + path);Path parent = dist.getParent();if (parent != null) {Files.createDirectories(parent);}Files.deleteIfExists(dist);return dist;}/*** 復制classpath下的文件到jar包的同級目錄下** @param location 相對路徑文件,例如kafka/kafka_client_jaas.conf* @return* @throws IOException*/public static String copy(String location) throws IOException {InputStream in = getResource("classpath:" + location);Path dist = getDistFile(location);Files.copy(in, dist);in.close();return dist.toAbsolutePath().toString();}}
8. 測試驗證

啟動項目
http://localhost:8080/services/axis2Service?wsdl

二、AXIS2服務端
2.1. 客戶端類
package com.gblfy.axis2.client;import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; import org.springframework.stereotype.Component;import javax.xml.namespace.QName;/*** axis2調用客戶端** @author gblfy* @date 2021-09-25*/ @Component public class Axis2WsClient {//默認超時時間20spublic static final Long timeOutInMilliSeconds = 2 * 20000L;/*** axis2調用客戶端** @param url 請求服務地址* @param nameSpace 命名空間* @param method 方法名* @param reqXml 請求報文* @param timeOutInMilliSeconds 超時時間* @return String 類型的響應報恩* @throws AxisFault 遇到異常則拋出不處理*/public static String sendWsMessage(String url, String nameSpace, String method, String reqXml, long timeOutInMilliSeconds) throws AxisFault {String resXml = null;try {EndpointReference targetEPR = new EndpointReference(url);RPCServiceClient sender = new RPCServiceClient();Options options = sender.getOptions();options.setTimeOutInMilliSeconds(timeOutInMilliSeconds); //超時時間20soptions.setTo(targetEPR);QName qName = new QName(nameSpace, method);Object[] param = new Object[]{reqXml};//這是針對返值類型的Class<?>[] types = new Class[]{String.class};Object[] response = sender.invokeBlocking(qName, param, types);resXml = response[0].toString();// System.out.println("響應報文:" + resXml);} catch (AxisFault e) {e.printStackTrace();throw e;}return resXml;}public static void main(String[] args) {try {String url = "http://localhost:8080/services/axis2Service?wsdl";String nameSpace = "http://service.axis2.gblfy.com";String method = "sayHello";String reqXml = "請求報文";String resXml = sendWsMessage(url, nameSpace, method, reqXml, timeOutInMilliSeconds);System.out.println("響應報文:" + resXml);} catch (AxisFault axisFault) {axisFault.printStackTrace();}} }
2.2. 服務調用測試

開源源碼.

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

總結

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

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