文章目录
- 一、AXIS2服务端
- 1. 版本选型
- 2.导入依赖
- 3. services.xml
- 4.Axis2配置类
- 5.服务接口
- 6.服务接口实现类
- 7. FileCopyUtils工具类
- 8. 测试验证
- 二、AXIS2服务端
- 2.1. 客户端类
- 2.2. 服务调用测试
- 开源源码.
一、AXIS2服务端
1. 版本选型
阿健/框架 | 版本 |
---|---|
spring-boot | 2.5.5 |
axis2 | 1.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