前言
最近公司要开发个接口,要用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里面的接口发布出来,此时客户端可以调用接口。