<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.ygang</groupId>
<artifactId>axisdemo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>2.2.0.RELEASE</version>
</parent>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<axis2.version>1.7.8</axis2.version>
</properties>
<dependencies>
<!-- spring-boot-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- axis2 -->
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-spring</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-http</artifactId>
<version>${axis2.version}</version>
</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-xmlbeans</artifactId>
<version>${axis2.version}</version>
</dependency>
</dependencies>
</project>
ServicePath/services/testService/META-INF/services.xml
,其中只有目录testService
的目录可变,其余目录都是固定
<?xml version="1.0" encoding="UTF-8"?>
<serviceGroup>
<!-- name为服务名,scope为生命周期,targetNamespace为命名空间 -->
<service name="testService" scope="application" targetNamespace="http://top.ygang.test/service">
<!-- 使用BeanName指定Service类 -->
<parameter name="SpringBeanName">testService</parameter>
<!-- 使用全类名指定Service类 -->
<!-- <parameter name="ServiceClass">top.ygang.axisdemo.services.TestService</parameter>-->
<!-- 命名空间 -->
<schema schemaNamespace="http://top.ygang.test/service"/>
<!-- 服务描述 -->
<description>
测试webservice
</description>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/ns/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
<messageReceiver mep="http://www.w3.org/ns/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</messageReceivers>
<parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier</parameter>
</service>
</serviceGroup>
如果使用jar打包项目,那么Axis2无法读取jar包内的配置文件,所以需要拷贝到jar包的同级目录中
public class ClasspathCopyUtil {
private static InputStream getResource(String location) throws IOException {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
InputStream in = resolver.getResource(location).getInputStream();
byte[] byteArray = IOUtils.toByteArray(in);
in.close();
return new ByteArrayInputStream(byteArray);
}
/**
* 获取项目所在文件夹的绝对路径
*
* @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 相对路径文件
* @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();
}
}
@Configuration
public class AxisWebserviceConfig {
@Value("${webservice.serviceName}")
private String serviceName;
@Value("${webservice.path}")
private String webservicePath;
private final static Logger log = LoggerFactory.getLogger(AxisWebserviceConfig.class);
@Bean
public ServletRegistrationBean<AxisServlet> axisServlet(){
ServletRegistrationBean<AxisServlet> registrationBean = new ServletRegistrationBean<>();
registrationBean.setServlet(new AxisServlet());
registrationBean.addUrlMappings(webservicePath + "/*");
//
String path = this.getClass().getResource("/ServicePath").getPath().toString();
if(path.toLowerCase().startsWith("file:")){
path = path.substring(5);
}
if(path.indexOf("!") != -1){
try{
ClasspathCopyUtil.copy("ServicePath/services/" + serviceName + "/META-INF/services.xml");
}catch (Exception e){
e.printStackTrace();
}
path = path.substring(0, path.lastIndexOf("/", path.indexOf("!"))) + "/ServicePath";
}
log.info("xml配置文件path={}","{" + path + "}");
registrationBean.addInitParameter("axis2.repository.path", path);
registrationBean.setLoadOnStartup(1);
return registrationBean;
}
@Bean
public ApplicationContextHolder getApplicationContextHolder(){
return new ApplicationContextHolder();
}
}
@Service("testService")
public class TestService {
public String test(String name){
return "hello axis";
}
}
@SpringBootApplication
@ServletComponentScan
public class Start extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Start.class,args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Start.class);
}
}
server:
port: 8080
webservice:
# 影响webservice服务url的前缀
path: /services
# services.xml目录名称,一般与services.xml中服务名一致
serviceName: testService
访问地址就是http://ip:port/yml中的path/服务名?wsdl
即:http://127.0.0.1:8080/services/testService?wsdl