本文档详细说明了使用SpringBoot集成axis发布WebService服务的步骤,供大家在开发的过程中参考
新建maven-web项目,archetype这里选择maven-archetype-webapp
pom.xml
的依赖添加如下:
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>4.0.0version>
dependency>
<dependency>
<groupId>axisgroupId>
<artifactId>axisartifactId>
<version>1.4version>
dependency>
<dependency>
<groupId>axisgroupId>
<artifactId>axis-wsdl4jartifactId>
<version>1.5.1version>
dependency>
<dependency>
<groupId>javax.mailgroupId>
<artifactId>mailartifactId>
<version>1.4.7version>
dependency>
新建一个OrderService接口,如下:
package com.iambest.study.axis.service;
public interface OrderService {
/**
* 下单接口
* @param userId 用户ID
* @param token 令牌
* @param orderInfo 订单信息-JSON格式
* @return
*/
String createOrder(String userId, String token, String orderInfo);
/**
* 支付接口
* @param userId 用户ID
* @param token 令牌
* @param paymentInfo 支付信息-JSON格式
* @return
*/
String payment(String userId, String token, String paymentInfo);
}
新建接口实现类,这里演示需要,仅对方法做简单实现,如下:
package com.iambest.study.axis.service.impl;
import com.iambest.study.axis.service.OrderService;
public class OrderServiceImpl implements OrderService {
@Override
public String createOrder(String userId, String token, String orderInfo) {
return "SUCCESS";
}
@Override
public String payment(String userId, String token, String paymentInfo) {
return "SUCCESS";
}
}
在webapp/WEB-INF目录下新建deploy.wsdd
文件,如下:
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="OrderService" provider="java:RPC">
<parameter name="className" value="com.iambest.study.axis.service.impl.OrderServiceImpl" />
<parameter name="allowedMethods" value="*" />
<namespace>http://com.iambest.study.axis.service/OrderServiceHttpSoap11Endpointnamespace>
service>
deployment>
菜单栏:Edit Configurations , 点击+号,选择Tomcat Server --> Local, 如下:
在Server标签页下,Application server: 点击Configure,选择你本地的Tomcat 服务器的路径,如下:
然后选择Deploymen标签页,点击+号,选择当前的项目即可
web.xml
文件新增如下的内容:
<servlet>
<servlet-name>AxisServletservlet-name>
<servlet-class>org.apache.axis.transport.http.AxisServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>AxisServletservlet-name>
<url-pattern>/services/*url-pattern>
servlet-mapping>
直接启动tomcat,浏览器访问:http://127.0.0.1:8080/axis_study/services
,显示如下的内容表示webservice服务发布成功了
切换到tomcat的webapps目录下,我的是mac电脑,路径如下:/Users/david/Developer/apache-tomcat-8.5.98/webapps/axis_study/WEB-INF
, 如果是别的电脑,则根据你的实际路径切换即可
执行如下的命令:
java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -l http://127.0.0.1:8080/axis_study/services deploy.wsdd
显示如下,表示生成文件成功:
将生成好的server-config.wsdd文件,复制到工程文件的WEB-INF目录下,和deploy.wsdd同级即可,然后重启tomcat,再次访问http://127.0.0.1:8080/axis_study/services
,显示如下表示成功生成server-config.wsdd文件
新建springboot工程,将1.1.2
章节的依赖添加到当前的pom.xml
文件中,并将接口和实现类复制到当前的工程中,如下:
在resources目录下,新建WEB-INF文件夹,将1.5.2
章节中生成的server-config.wsdd
文件复制到WEB-INF
目录下
新建AxisServiceConfig配置类,内容如下:
package com.iambest.study.axis.config;
import org.apache.axis.transport.http.AxisServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AxisServiceConfig {
@Bean
public ServletRegistrationBean<AxisServlet> axisServletServletRegistrationBean() {
ServletRegistrationBean<AxisServlet> axisServletServletRegistrationBean =
new ServletRegistrationBean<>(new AxisServlet(),"/services/*");
axisServletServletRegistrationBean.setName("AxisServlet");
axisServletServletRegistrationBean.setLoadOnStartup(1);
return axisServletServletRegistrationBean;
}
}
新建EngineConfigurationFactoryServlet类,必须放到org.apache.axis.configuration
包路径下,内容入下:
package org.apache.axis.configuration;
import java.io.InputStream;
import javax.servlet.ServletConfig;
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;
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);
}
/**
* Get a default server engine configuration in a servlet environment.
*
* @param cfg a ServletContext
* @return a server EngineConfiguration
*/
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";
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);
}
}
/**
* Fall back to config file packaged with AxisEngine
*/
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;
}
}
最终的项目结构如下图所示:
启动springboot应用,并访问:http://127.0.0.1:8080/services
路径,显示内容如下表示发布服务成功:
通过wget命令获取wsdl文件
wget -O OrderService.wsdl http://127.0.0.1:8080/services/OrderService?wsdl
导入刚才的wsdl文件后,发送请求信息,返回如下的信息,表示成功: