文章目录
一、先用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、新建接口和实现类
新建接口类:
packagecn.mytest.service;publicinterfaceTestService{publicStringsayHello(String name);publicStringadd(String info);}
新建实现类:
packagecn.mytest.service.impl;importcn.mytest.service.TestService;publicclassTestServiceImplimplementsTestService{@OverridepublicStringsayHello(String name){return"成功调用...";}@OverridepublicStringadd(String info){return"添加:"+info;}}
3、新建WSDD(Web Service Deployment Descriptor)文件
在工程WEB-INF目录新建一个TestWebService.wsdd文件,添加配置信息。
<deploymentxmlns="http://xml.apache.org/axis/wsdd/"xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"><!-- name: 服务名可以随便写。 --><servicename="TestService"provider="java:RPC"><parametername="className"value="cn.mytest.service.impl.TestServiceImpl"/><!-- value表示哪些方法需要发布,*表示全部的public方法。 --><!-- 如果想指定方法名,方法名与方法名用逗号隔开 --><parametername="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配置类:
packagecn.mytest.service.config;importorg.apache.axis.transport.http.AxisServlet;importorg.springframework.boot.web.servlet.ServletRegistrationBean;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;@ConfigurationpublicclassAxisServiceConfig{@BeanpublicServletRegistrationBean<AxisServlet>axisServletServletRegistrationBean(){ServletRegistrationBean<AxisServlet> axisServletServletRegistrationBean =newServletRegistrationBean<>(newAxisServlet(),"/services/*");
axisServletServletRegistrationBean.setName("AxisServlet");
axisServletServletRegistrationBean.setLoadOnStartup(1);return axisServletServletRegistrationBean;}}
新建EngineConfigurationFactoryServlet配置类:
需要新建并放到org.apache.axis.configuration包路径下。
packageorg.apache.axis.configuration;importjava.io.InputStream;importjavax.servlet.ServletConfig;importorg.apache.axis.AxisProperties;importorg.apache.axis.ConfigurationException;importorg.apache.axis.EngineConfiguration;importorg.apache.axis.EngineConfigurationFactory;importorg.apache.axis.components.logger.LogFactory;importorg.apache.axis.server.AxisServer;importorg.apache.axis.utils.ClassUtils;importorg.apache.axis.utils.Messages;importorg.apache.commons.logging.Log;publicclassEngineConfigurationFactoryServletextendsEngineConfigurationFactoryDefault{protectedstaticLog log =LogFactory.getLog(EngineConfigurationFactoryServlet.class.getName());privateServletConfig cfg;publicstaticEngineConfigurationFactorynewFactory(Object param){return(param instanceofServletConfig)?newEngineConfigurationFactoryServlet((ServletConfig) param):null;}protectedEngineConfigurationFactoryServlet(ServletConfig conf){super();this.cfg = conf;}@OverridepublicEngineConfigurationgetServerEngineConfig(){returngetServerEngineConfig(cfg);}/**
* Get a default server engine configuration in a servlet environment.
*
* @param cfg a ServletContext
* @return a server EngineConfiguration
*/privatestaticEngineConfigurationgetServerEngineConfig(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 =newFileProvider(iss);}if(config ==null){
log.error(Messages.getMessage("servletEngineWebInfError03",""));}if(config ==null&& realWebInfPath !=null){try{
config =newFileProvider(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 =newFileProvider(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
版权归原作者 追风★少年 所有, 如有侵权,请联系我们删除。