文章目录
一、先用IDEA通过Maven构建一个Web项目1、构建Web项目2、新建接口和实现类3、新建WSDD(Web Service Deployment Descriptor)文件4、配置Tomcat启动服务5、通过java命令生成server-config.wsdd文件6、通过SoapUi工具测试 二、SpringBoot集成Axis构建WebService服务1、新建SpringBoot项目2、新建配置类3、测试服务
一、先用IDEA通过Maven构建一个Web项目
1、构建Web项目
在pom文件中添加依赖:
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.0</version></dependency><dependency> <groupId>axis</groupId> <artifactId>axis</artifactId> <version>1.4</version></dependency><dependency> <groupId>axis</groupId> <artifactId>axis-wsdl4j</artifactId> <version>1.5.1</version></dependency><dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version></dependency>
2、新建接口和实现类
新建接口类:
package cn.mytest.service;public interface TestService { public String sayHello(String name); public String add(String info);}
新建实现类:
package cn.mytest.service.impl;import cn.mytest.service.TestService;public class TestServiceImpl implements TestService { @Override public String sayHello(String name) { return "成功调用..."; } @Override public String add(String info) { return "添加:"+info; }}
3、新建WSDD(Web Service Deployment Descriptor)文件
在工程WEB-INF目录新建一个TestWebService.wsdd文件,添加配置信息。
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <!-- name: 服务名可以随便写。 --> <service name="TestService" provider="java:RPC"> <parameter name="className" value="cn.mytest.service.impl.TestServiceImpl" /> <!-- value表示哪些方法需要发布,*表示全部的public方法。 --> <!-- 如果想指定方法名,方法名与方法名用逗号隔开 --> <parameter name="allowedMethods" value="*" /> <!-- 指定命名空间 --> <namespace>http://cn.mytest.service/TestServiceHttpSoap11Endpoint</namespace> </service></deployment>
4、配置Tomcat启动服务
在web.xml文件中添加对应的配置信息:
<servlet> <servlet-name>AxisServlet</servlet-name> <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class></servlet><servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern></servlet-mapping>
启动Tomcat,访问地址:
http://localhost:8081/TestWebService/services 说明发布成功。
5、通过java命令生成server-config.wsdd文件
进入到tomcat目录下:
执行命令:
java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -l http://127.0.0.1:8081/TestWebService/services TestWebService.wsdd
表示文件生成成功:
再访问地址:
http://localhost:8081/TestWebService/services 生成了对应的接口信息。
6、通过SoapUi工具测试
接口返回了信息,说明调用成功:
二、SpringBoot集成Axis构建WebService服务
1、新建SpringBoot项目
新建完,复制上面的接口和实现类过来。
添加对应的依赖:
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.0</version></dependency><dependency> <groupId>axis</groupId> <artifactId>axis</artifactId> <version>1.4</version></dependency><dependency> <groupId>axis</groupId> <artifactId>axis-wsdl4j</artifactId> <version>1.5.1</version></dependency><dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version></dependency>
拷贝上面生成过的配置文件server-config.wsdd到resources/WEB-INF下:
2、新建配置类
新建AxisServiceConfig配置类:
package cn.mytest.service.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;@Configurationpublic 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; }}
项目结构:
3、测试服务
启动服务并访问:
通过SoapUI测试:
通过wget命令获取wsdl文件:
下载文件后,可以导入到soapui中。
wget -O TestService.wsdl http://localhost:8080/services/TestService?wsdl