SpringBoot+Axis2搭建WebService服务端

本文详细介绍了如何在SpringBoot项目中使用Axis2搭建WebService服务端,包括配置services.xml和通过Java代码添加servlet的过程,以及解决路径问题的方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot+Axis2搭建WebService服务端

之前用过Spring + Axis2搭建过WebService项目,网上也有很多资料教程,最近需要在一个SpringBoot项目中添加Axis2的服务端,在网上找了很久,没有找到相关教程,最终经过大神朋友的指点,终于添加成功了,在此记录一下,防止忘记。
参考资料:https://www.cnkirito.moe/servlet-explore/?utm_source=tuicool&utm_medium=referral

改变一:添加services.xml时

spring项目时,有WEB-INF目录,services.xml配置文件需要放在此目录下,但是springboot下没有了这个目录,需要在resources目录下新建WEB-INF目录,然后把services.xml的相关目录放到WEB-INF下:
在这里插入图片描述

改变二:添加servlet时

spring项目时,可以在web.xml中配置,但是springboot项目没有了这个文件,我们可以通过java代码来实现添加:

	@Bean
	public ServletRegistrationBean helloWorldServlet() {
	    ServletRegistrationBean helloWorldServlet = new ServletRegistrationBean();
	    helloWorldServlet.setServlet(new AxisServlet());//这里的AxisServlet就是web.xml中的org.apache.axis2.transport.http.AxisServlet
	    helloWorldServlet.addUrlMappings("/services/*");
	    //通过默认路径无法找到services.xml,这里需要指定一下路径,且必须是绝对路径
	    helloWorldServlet.addInitParameter("axis2.repository.path", this.getClass().getResource("/WEB-INF").getPath().toString());
	    helloWorldServlet.setLoadOnStartup(1);
	    return helloWorldServlet;
	}

后续

在实际部署后发现获得的路径里带有“!”,
在这里插入图片描述
导致Axis2无法正常使用这个路径,经过各种查资料,最终没有找到比较好的解决办法,只能用最笨的办法拷贝到项目所在的目录下来用了,解决方案如下:

	@Bean
	public ServletRegistrationBean helloWorldServlet() {
	    ServletRegistrationBean helloWorldServlet = new ServletRegistrationBean();
	    helloWorldServlet.setServlet(new AxisServlet());//这里的AxisServlet就是web.xml中的org.apache.axis2.transport.http.AxisServlet
	    helloWorldServlet.addUrlMappings("/services/*");
	    //通过默认路径无法找到services.xml,这里需要指定一下路径,且必须是绝对路径
	    String path = this.getClass().getResource("/WEB-INF").getPath().toString();
	    LogUtil.info("The original path:" + path);
	    if(path.toLowerCase().startsWith("file:")){
	    	LogUtil.info("去掉前面的“file:”!");
	    	path = path.substring(5);
	    }
	    if(path.indexOf("!") != -1){
	    	try {
	    		LogUtil.info("将WEB-INF/services/MyWebService/META-INF/services.xml文件拷贝到jar包同级目录下!");
				FileCopyUtils.copy("WEB-INF/services/MyWebService/META-INF/services.xml");
			} catch (IOException e) {
				LogUtil.error(e);
			}
	    	LogUtil.info("jar包运行!查找jar包同级目录下的“/WEB-INF”目录");
	    	path = path.substring(0, path.lastIndexOf("/", path.indexOf("!"))) + "/WEB-INF";
	    }
	    LogUtil.info("The final path:" + path);
	    helloWorldServlet.addInitParameter("axis2.repository.path", path);
	    helloWorldServlet.setLoadOnStartup(1);
	    return helloWorldServlet;
	}

其中FileCopyUtils类是在网上找到的一个前辈写的:https://blog.csdn.net/sz85850597/article/details/81116755
另外发现“/WEB-INF”目录名是可以改的,不是非要叫这个名字。
技术有限,暂时无法完美解决,如有好的解决方案,请多指点。

附:源码 链接: https://pan.baidu.com/s/1zEo9pIQlkkLpeIBVzQ1yZw 提取码: kp9j

再后续

services.xml文件存放的目录问题,必须包含的目录结构:“axis2.repository.path”参数目录/services/自定义目录/META-INF/services.xml
其中services、META-INF、services.xml三个名字应该是不能修改,改了找不到服务了

实现Spring Boot整合Axis1.4实现WebService服务端,可以按照以下步骤进行: 1. 在pom.xml文件中添加Axis1.4依赖: ``` <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency> ``` 2. 创建一个WebService接口,并在接口上添加@WebService注解: ``` @WebService public interface UserService { String sayHello(String name); } ``` 3. 创建一个WebService接口的实现类,并在实现类上添加@WebService(endpointInterface = "com.example.demo.UserService")注解: ``` @WebService(endpointInterface = "com.example.demo.UserService") public class UserServiceImpl implements UserService { @Override public String sayHello(String name) { return "Hello, " + name + "!"; } } ``` 4. 在Spring Boot的配置文件application.properties中添加Axis1.4的配置: ``` # Axis1.4配置 axis.servletPath=/services/* ``` 5. 创建一个AxisServlet的注册类,并在类上添加@Configuration和@EnableWebMvc注解: ``` @Configuration @EnableWebMvc public class AxisServletRegistration { @Bean public ServletRegistrationBean<AxisServlet> axisServlet() { ServletRegistrationBean<AxisServlet> registration = new ServletRegistrationBean<>(new AxisServlet(), "/services/*"); registration.addInitParameter("axis.servicesPath", "/WEB-INF/services"); registration.addInitParameter("axis.wsddPath", "/WEB-INF/server-config.wsdd"); return registration; } } ``` 6. 启动Spring Boot应用程序,访问http://localhost:8080/services/UserService?wsdl,可以看到WebService服务端已经成功启动。 以上就是Spring Boot整合Axis1.4实现WebService服务端的全部步骤。
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值