當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
SpringBoot2 整合 AXIS2 服务端和客户端
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot2 整合 AXIS2 服务端和客户端
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 一、AXIS2服務(wù)端
- 1. 版本選型
- 2.導(dǎo)入依賴
- 3. services.xml
- 4.Axis2配置類
- 5.服務(wù)接口
- 6.服務(wù)接口實(shí)現(xiàn)類
- 7. FileCopyUtils工具類
- 8. 測(cè)試驗(yàn)證
- 二、AXIS2服務(wù)端
- 2.1. 客戶端類
- 2.2. 服務(wù)調(diào)用測(cè)試
- 開(kāi)源源碼.
一、AXIS2服務(wù)端
1. 版本選型
| spring-boot | 2.5.5 |
| axis2 | 1.7.9 |
2.導(dǎo)入依賴
<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
首先創(chuàng)建基本路徑:WEB-INF/services/axis2/META-INF/services.xml
注:路徑名必須和上面一致
services.xml內(nèi)容
4.Axis2配置類
Axis2WebServiceConfiguration是webservice最主要的配置類,主要作用為讀取services.xml配置文件,內(nèi)容如下:
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配置類,用于設(shè)置AxisServlet和訪問(wèn)讀取services.xml文件** @author gblfy* @date 2021-09-25*/ @Configuration public class Axis2WebServiceConfiguration {//服務(wù)訪問(wèn)前綴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參數(shù)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);// 通過(guò)默認(rèn)路徑無(wú)法找到services.xml,這里需要指定一下路徑,且必須是絕對(duì)路徑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.服務(wù)接口
package com.gblfy.axis2.service;/*** axis2接口類** @author gblfy* @date 2021-09-25*/ public interface Axis2Service {public String sayHello(String name); }6.服務(wù)接口實(shí)現(xiàn)類
package com.gblfy.axis2.service.impl;import com.gblfy.axis2.service.Axis2Service; import org.springframework.stereotype.Service;/*** axis2接口實(shí)現(xiàn)類** @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的調(diào)用,代碼如下:
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內(nèi)的文件復(fù)制到j(luò)ar包外的同級(jí)目錄下** @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();}}}/*** 獲取項(xiàng)目所在文件夾的絕對(duì)路徑** @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;}/*** 復(fù)制classpath下的文件到j(luò)ar包的同級(jí)目錄下** @param location 相對(duì)路徑文件,例如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. 測(cè)試驗(yàn)證
啟動(dòng)項(xiàng)目
http://localhost:8080/services/axis2Service?wsdl
二、AXIS2服務(wù)端
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調(diào)用客戶端** @author gblfy* @date 2021-09-25*/ @Component public class Axis2WsClient {//默認(rèn)超時(shí)時(shí)間20spublic static final Long timeOutInMilliSeconds = 2 * 20000L;/*** axis2調(diào)用客戶端** @param url 請(qǐng)求服務(wù)地址* @param nameSpace 命名空間* @param method 方法名* @param reqXml 請(qǐng)求報(bào)文* @param timeOutInMilliSeconds 超時(shí)時(shí)間* @return String 類型的響應(yīng)報(bào)恩* @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); //超時(shí)時(shí)間20soptions.setTo(targetEPR);QName qName = new QName(nameSpace, method);Object[] param = new Object[]{reqXml};//這是針對(duì)返值類型的Class<?>[] types = new Class[]{String.class};Object[] response = sender.invokeBlocking(qName, param, types);resXml = response[0].toString();// System.out.println("響應(yīng)報(bào)文:" + 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 = "請(qǐng)求報(bào)文";String resXml = sendWsMessage(url, nameSpace, method, reqXml, timeOutInMilliSeconds);System.out.println("響應(yīng)報(bào)文:" + resXml);} catch (AxisFault axisFault) {axisFault.printStackTrace();}} }2.2. 服務(wù)調(diào)用測(cè)試
開(kāi)源源碼.
https://gitee.com/gb_90/unified-access-center
總結(jié)
以上是生活随笔為你收集整理的SpringBoot2 整合 AXIS2 服务端和客户端的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mybatisplus代码生成器3.5.
- 下一篇: docker Gitlab14.5.0