最近在做一个老旧工程的迁移,其中一个服务端接口是使用 WebService 做的,而且用的是 Axis1.4 这种老的不能再老的框架。经过一番探索和研究,终于成功迁移到Springboot工程里,下面跟着我一步步操作。
Axis1.4 的入口配置文件是 server-config.wsdd
,在 resources 中创建一个 WEB-INF 文件夹,将配置文件拷贝到其中。
我们看一下配置文件的内容:
< ?xml version="1.0" encoding="UTF-8"? >
< deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java" >
< handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/ >
< handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/ >
< transport name="http" >
< requestFlow >
< handler type="URLMapper"/ >
< /requestFlow >
< /transport >
< transport name="local" >
< responseFlow >
< handler type="LocalResponder"/ >
< /responseFlow >
< /transport >
< !-- 示例接口 -- >
< service name="DemoService" provider="java:RPC" >
< parameter name="className" value="com.test.webservice.DemoService"/ >
< parameter name="allowedMethods" value="*"/ >
< /service >
< /deployment >
主要看最后一段:声明了一个示例接口:服务名为DemoService
,映射的类为com.test.webservice.DemoService
,方法名为*
(通配符代表与类中的方法名一样)
简简单单的一个接口类:
package com.test.webservice;
import org.springframework.stereotype.Component;
@Component
public class DemoService {
public String test() {
return "这是一个Axis1.4接口。";
}
}
我们如何把这个接口发布出去呢,接着往下看。
我这里用的是 gradle 构建的:
implementation('org.apache.axis:axis:1.4')
implementation('org.apache.axis:axis-jaxrpc:1.4')
implementation('axis:axis-wsdl4j:1.5.1')
implementation('commons-discovery:commons-discovery:0.5')
创建一个Servlet类,继承AxisServlet:
package com.test.webservice.servlet;
import org.apache.axis.transport.http.AxisServlet;
import javax.servlet.annotation.WebServlet;
@WebServlet(
urlPatterns = "/axis-services/*",
loadOnStartup = 1,
name = "AxisServlet"
)
public class MyAxisServlet extends AxisServlet {
}
注意,Axis默认的过滤 url 是/services/
,而我这里将 urlPatterns 配置为/axis-services/
,是因为工程中同时使用了 Cxf框架 ,如果使用默认配置,会与 Cxf 冲突。
网上拷贝的配置工厂类:
package org.apache.axis.configuration;
import org.apache.axis.AxisProperties;
import org.apache.axis.ConfigurationException;
import org.apache.axis.EngineConfiguration;
import org.apache.axis.EngineConfigurationFactory;
import org.apache.axis.components.logger.LogFactory;
import org.apache.axis.server.AxisServer;
import org.apache.axis.utils.ClassUtils;
import org.apache.axis.utils.Messages;
import org.apache.commons.logging.Log;
import javax.servlet.ServletConfig;
import java.io.InputStream;
public class EngineConfigurationFactoryServlet extends EngineConfigurationFactoryDefault {
protected static Log log = LogFactory.getLog(EngineConfigurationFactoryServlet.class.getName());
private ServletConfig cfg;
public static EngineConfigurationFactory newFactory(Object param) {
return (param instanceof ServletConfig)
? new EngineConfigurationFactoryServlet((ServletConfig) param)
: null;
}
protected EngineConfigurationFactoryServlet(ServletConfig conf) {
super();
this.cfg = conf;
}
@Override
public EngineConfiguration getServerEngineConfig() {
return getServerEngineConfig(cfg);
}
private static EngineConfiguration getServerEngineConfig(ServletConfig cfg) {
String configFile = cfg.getInitParameter(OPTION_SERVER_CONFIG_FILE);
if (configFile == null) {
configFile =
AxisProperties.getProperty(OPTION_SERVER_CONFIG_FILE);
}
if (configFile == null) {
configFile = SERVER_CONFIG_FILE;
}
String appWebInfPath = "/WEB-INF";
//由于部署方式变更为jar部署,此处不可以使用改方式获取路径
// ServletContext ctx = cfg.getServletContext();
// String realWebInfPath = ctx.getRealPath(appWebInfPath);
FileProvider config = null;
String realWebInfPath = EngineConfigurationFactoryServlet.class.getResource(appWebInfPath).getPath();
InputStream iss = ClassUtils.getResourceAsStream(EngineConfigurationFactoryServlet.class, appWebInfPath + "/" + SERVER_CONFIG_FILE);
if (iss != null) {
config = new FileProvider(iss);
}
if (config == null) {
log.error(Messages.getMessage("servletEngineWebInfError03", ""));
}
if (config == null && realWebInfPath != null) {
try {
config = new FileProvider(realWebInfPath, configFile);
} catch (ConfigurationException e) {
log.error(Messages.getMessage("servletEngineWebInfError00"), e);
}
}
if (config == null) {
log.warn(Messages.getMessage("servletEngineWebInfWarn00"));
try {
InputStream is =
ClassUtils.getResourceAsStream(AxisServer.class,
SERVER_CONFIG_FILE);
config = new FileProvider(is);
} catch (Exception e) {
log.error(Messages.getMessage("servletEngineWebInfError02"), e);
}
}
return config;
}
}
有两点需要注意:
org.apache.axis.configuration
包下,否则不生效,别问为什么,我也不知道...String appWebInfPath = "/WEB-INF";
指向的文件夹就是一开始创建的 WEB-INF
在启动类加上注解 @ServletComponentScan
@SpringBootApplication
@ServletComponentScan
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
启动程序,访问 http://localhost:8080/axis-services
,会显示已发布的 WebService 接口信息,示例接口地址为:http://localhost:8080/axis-services/DemoService?wsdl
。
全部0条评论
快来发表一下你的评论吧 !