springboot整合axis1.4搭建服务端
程序员文章站
2022-12-05 17:08:49
前言 最近公司要开发个接口,要用webservices接口实现,而且使用的是axis1.4框架,webservices和axis这两个东西我之前都没接触过,而且axis1.4这个框架06年就不再维护了,没办法,客户就是上帝。上网查了一圈,基本都是spring整合axis的,而公司用的是springb ......
前言
最近公司要开发个接口,要用webservices接口实现,而且使用的是axis1.4框架,webservices和axis这两个东西我之前都没接触过,而且axis1.4这个框架06年就不再维护了,没办法,客户就是上帝。上网查了一圈,基本都是spring整合axis的,而公司用的是springboot,比较头疼,在此记录下搭建过程。
一、引入依赖
1 <dependency> 2 <groupid>org.apache.axis</groupid> 3 <artifactid>axis</artifactid> 4 <version>1.4</version> 5 </dependency> 6 7 <dependency> 8 <groupid>axis</groupid> 9 <artifactid>axis-jaxrpc</artifactid> 10 <version>1.4</version> 11 </dependency> 12 13 <dependency> 14 <groupid>commons-discovery</groupid> 15 <artifactid>commons-discovery</artifactid> 16 <version>0.2</version> 17 </dependency> 18 <dependency> 19 <groupid>wsdl4j</groupid> 20 <artifactid>wsdl4j</artifactid> 21 <version>1.6.3</version> 22 </dependency>
二、写个接口以及实现类
接口:
1 public interface helloservice { 2 public string sayhello(string info); 3 4 }
实现类:
1 public class helloserviceimpl implements helloservice { 2 public string sayhello(string info) { 3 return "sayhello:"+info; 4 } 5 }
三、创建资源文件server-config.wsdd
1 <?xml version="1.0" encoding="utf-8"?> 2 <deployment xmlns="http://xml.apache.org/axis/wsdd/" 3 xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> 4 <handler type="java:org.apache.axis.handlers.http.urlmapper" 5 name="urlmapper" /> 6 7 <!--要告诉别人的接口名--> 8 <service name="helloserviceimpl" provider="java:rpc"> 9 <!--这个是 实现类--> 10 <parameter name="classname" value="com.codefish.javalab.ws.server.helloserviceimpl" /> 11 <!--这是是暴露的方法名 比如可以值暴露一个--> 12 <parameter name="allowedmethods" value="sayhello" /> 13 <!--这是是暴露的方法名 也可以用* 表示暴露全部的public方法--> 14 <!--<parameter name="allowedmethods" value="*" />--> 15 </service> 16 17 <transport name="http"> 18 <requestflow> 19 <handler type="urlmapper" /> 20 </requestflow> 21 </transport> 22 23 </deployment>
四、添加servlet过滤规则
新建com.example.servlet.webservlet,继承axisservlet。
1 package com.example.servlet; 2 import org.apache.axis.transport.http.axisservlet; 3 @javax.servlet.annotation.webservlet( 4 urlpatterns = "/services/*", 5 loadonstartup = 1, 6 name = "axisservlet" 7 ) 8 public class webservlet extends axisservlet { 9 10 }
五、重写axis的配置工厂信息
因为我想要以jar包形式发布,所以需要重写engineconfigurationfactory类,否则会访问不到。新建org.apache.axis.configuration.engineconfigurationfactoryservlet,继承engineconfigurationfactorydefault。更改的是getserverengineconfig(servletconfig cfg)方法。(注意:该类需要放在org.apache.axis.configuration包下,我尝试放在其他路径下无效,如有大神知道,还望告知)
1 /* 2 * copyright 2002-2004 the apache software foundation. 3 * 4 * licensed under the apache license, version 2.0 (the "license"); 5 * you may not use this file except in compliance with the license. 6 * you may obtain a copy of the license at 7 * 8 * http://www.apache.org/licenses/license-2.0 9 * 10 * unless required by applicable law or agreed to in writing, software 11 * distributed under the license is distributed on an "as is" basis, 12 * without warranties or conditions of any kind, either express or implied. 13 * see the license for the specific language governing permissions and 14 * limitations under the license. 15 */ 16 17 package org.apache.axis.configuration; 18 19 import org.apache.axis.axisproperties; 20 import org.apache.axis.configurationexception; 21 import org.apache.axis.engineconfiguration; 22 import org.apache.axis.engineconfigurationfactory; 23 import org.apache.axis.components.logger.logfactory; 24 import org.apache.axis.server.axisserver; 25 import org.apache.axis.utils.classutils; 26 import org.apache.axis.utils.messages; 27 import org.apache.commons.logging.log; 28 29 import javax.servlet.servletconfig; 30 import java.io.inputstream; 31 32 /** 33 * this is a default implementation of servletengineconfigurationfactory. 34 * it is user-overrideable by a system property without affecting 35 * the caller. if you decide to override it, use delegation if 36 * you want to inherit the behaviour of this class as using 37 * class extension will result in tight loops. that is, your 38 * class should implement engineconfigurationfactory and keep 39 * an instance of this class in a member field and delegate 40 * methods to that instance when the default behaviour is 41 * required. 42 * 43 * @author richard a. sitze 44 * @author davanum srinivas (dims@apache.org) 45 */ 46 public class engineconfigurationfactoryservlet 47 extends engineconfigurationfactorydefault { 48 protected static log log = 49 logfactory.getlog(engineconfigurationfactoryservlet.class.getname()); 50 51 private servletconfig cfg; 52 53 /** 54 * creates and returns a new engineconfigurationfactory. 55 * if a factory cannot be created, return 'null'. 56 * <p> 57 * the factory may return non-null only if: 58 * - it knows what to do with the param (param instanceof servletcontext) 59 * - it can find it's configuration information 60 * 61 * @see engineconfigurationfactoryfinder 62 */ 63 public static engineconfigurationfactory newfactory(object param) { 64 /** 65 * default, let this one go through if we find a servletcontext. 66 * 67 * the real reason we are not trying to make any 68 * decision here is because it's impossible 69 * (without refactoring fileprovider) to determine 70 * if a *.wsdd file is present or not until the configuration 71 * is bound to an engine. 72 * 73 * fileprovider/engineconfiguration pretend to be independent, 74 * but they are tightly bound to an engine instance... 75 */ 76 return (param instanceof servletconfig) 77 ? new engineconfigurationfactoryservlet((servletconfig) param) 78 : null; 79 } 80 81 /** 82 * create the default engine configuration and detect whether the user 83 * has overridden this with their own. 84 */ 85 protected engineconfigurationfactoryservlet(servletconfig conf) { 86 super(); 87 this.cfg = conf; 88 } 89 90 /** 91 * get a default server engine configuration. 92 * 93 * @return a server engineconfiguration 94 */ 95 public engineconfiguration getserverengineconfig() { 96 return getserverengineconfig(cfg); 97 } 98 99 /** 100 * get a default server engine configuration in a servlet environment. 101 * 102 * @param cfg a servletcontext 103 * @return a server engineconfiguration 104 */ 105 private static engineconfiguration getserverengineconfig(servletconfig cfg) { 106 107 // respect the system property setting for a different config file 108 string configfile = cfg.getinitparameter(option_server_config_file); 109 if (configfile == null) 110 configfile = 111 axisproperties.getproperty(option_server_config_file); 112 if (configfile == null) { 113 configfile = server_config_file; 114 } 115 116 /** 117 * flow can be confusing. here is the logic: 118 * 1) make all attempts to open resource if it exists 119 * - if it exists as a file, open as file (r/w) 120 * - if not a file, it may still be accessable as a stream (r) 121 * (env will handle security checks). 122 * 2) if it doesn't exist, allow it to be opened/created 123 * 124 * now, the way this is done below is: 125 * a) if the file does not exist, attempt to open as a stream (r) 126 * b) open named file (opens existing file, creates if not avail). 127 */ 128 129 /* 130 * use the web-inf directory 131 * (so the config files can't get snooped by a browser) 132 */ 133 string appwebinfpath = "/web-inf"; 134 //由于部署方式变更为jar部署,此处不可以使用改方式获取路径 135 // servletcontext ctx = cfg.getservletcontext(); 136 // string realwebinfpath = ctx.getrealpath(appwebinfpath); 137 138 fileprovider config = null; 139 string realwebinfpath = engineconfigurationfactoryservlet.class.getresource(appwebinfpath).getpath(); 140 141 /** 142 * if path/file doesn't exist, it may still be accessible 143 * as a resource-stream (i.e. it may be packaged in a jar 144 * or war file). 145 */ 146 inputstream iss = classutils.getresourceasstream(engineconfigurationfactoryservlet.class, appwebinfpath+"/" + server_config_file); 147 if (iss != null) { 148 // fileprovider assumes responsibility for 'is': 149 // do not call is.close(). 150 config = new fileprovider(iss); 151 } 152 153 if (config == null) { 154 log.error(messages.getmessage("servletenginewebinferror03", "")); 155 } 156 157 /** 158 * couldn't get data or file does exist. 159 * if we have a path, then attempt to either open 160 * the existing file, or create an (empty) file. 161 */ 162 if (config == null && realwebinfpath != null) { 163 try { 164 config = new fileprovider(realwebinfpath, configfile); 165 } catch (configurationexception e) { 166 log.error(messages.getmessage("servletenginewebinferror00"), e); 167 } 168 } 169 170 /** 171 * fall back to config file packaged with axisengine 172 */ 173 if (config == null) { 174 log.warn(messages.getmessage("servletenginewebinfwarn00")); 175 try { 176 inputstream is = 177 classutils.getresourceasstream(axisserver.class, 178 server_config_file); 179 config = new fileprovider(is); 180 181 } catch (exception e) { 182 log.error(messages.getmessage("servletenginewebinferror02"), e); 183 } 184 } 185 186 return config; 187 } 188 }
六、配置application
添加注解 @servletcomponentscan
@springbootapplication @servletcomponentscan //扫描自定义的webfilter和weblistener,否则无法扫描到 public class application { public static void main(string[] args) { springapplication.run(application.class, args); } }
启动程序,访问http://localhost:8080/services,会将server-config.wsdd里面的接口发布出来,此时客户端可以调用接口。
推荐阅读
-
SpringBoot整合SpringCloud搭建分布式应用
-
SpringBoot2 整合Nacos组件,环境搭建和入门案例详解
-
springboot整合axis1.4搭建服务端
-
如何利用IDEA搭建SpringBoot项目整合mybatis实现简单的登录功能
-
搭建SpringBoot整合ssm框架
-
详解mall整合SpringBoot+MyBatis搭建基本骨架
-
从零搭建SpringBoot2.X整合Redis框架的详细教程
-
【弄nèng - Activiti6】Springboot搭建Activiti整合流程设计器
-
SpringBoot整合Eureka搭建微服务
-
SpringBoot2 整合Nacos组件,环境搭建和入门案例详解