Spring boot 集成Axis1.4 ,使用wsdd文件发布webservice(底部有demo链接)
前言
-
说起web service最近几年restful大行其道,大有取代传统soap web service的趋势,但是一些特有或相对老旧的系统依然使用了传统的soap web service,例如在iptv和ott行业中进行系统交互的接口协议。
-
目前就遇到了这种情况,线上环境对接的系统统一使用soap web service接口,现在需要将原有系统中对外对接的部分抽出来,建设一个新的组件,使用当前比较火的框架为spring boot。
-
spring整合webservice本来十分简单,但是因为使用了spring
boot,不想用之前的xml一堆配置的方式和想要以jar发布,那么能否按照Spring boot的风格优雅的进行整合呢? -
答案当然是肯定的,但是遍查网上几乎没有这方面的资料,折腾过后觉得还是有必要记录一下。
创建工程
- 使用idea或者myeclipse或者eclipse或者其他的工具来创建项目,此处使用idea创建。File–>New–>Project,选择Spring
Initializr 创建工程,一路默认即可。注:此处使用的版本为1.5.3.RELEASE
添加依赖
首先要添加相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--log4g2 配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- axis 依赖开始 -->
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.2</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
<!-- axis 依赖结束 -->
</dependencies>
根据WSDL使用axis创建代码
此处自行google,将生成好的代码放入到com.example.webservice.c2包中
创建资源文件server-config.wsdd
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<globalConfiguration>
<parameter name="sendMultiRefs" value="true"/>
<parameter name="disablePrettyXML" value="true"/>
<parameter name="adminPassword" value="admin"/>
<parameter name="attachments.Directory" value=""/>
<parameter name="dotNetSoapEncFix" value="true"/>
<parameter name="enableNamespacePrefixOptimization" value="false"/>
<parameter name="sendXMLDeclaration" value="true"/>
<parameter name="sendXsiTypes" value="true"/>
<parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/>
<requestFlow>
<handler type="java:org.apache.axis.handlers.JWSHandler">
<parameter name="scope" value="session"/>
</handler>
<handler type="java:org.apache.axis.handlers.JWSHandler">
<parameter name="scope" value="request"/>
<parameter name="extension" value=".jwr"/>
</handler>
</requestFlow>
</globalConfiguration>
<handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
<handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>
<handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
<service name="ctms" provider="java:RPC" style="rpc" use="encoded">
<parameter name="wsdlTargetNamespace" value="iptv"/>
<parameter name="wsdlServiceElement" value="CSPRequestService"/>
<parameter name="schemaUnqualified" value="iptv"/>
<parameter name="wsdlServicePort" value="ctms"/>
<parameter name="className" value="com.example.webservice.c2.ctms.CtmsSoapBindingSkeleton"/>
<parameter name="wsdlPortType" value="CSPRequest"/>
<parameter name="typeMappingVersion" value="1.2"/>
<parameter name="allowedMethods" value="*"/>
<typeMapping
xmlns:ns="iptv"
qname="ns:CSPResult"
type="java:com.example.webservice.c2.ctms.CSPResult"
serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
/>
</service>
<service name="ctmsResp" provider="java:RPC" style="rpc" use="encoded">
<parameter name="wsdlTargetNamespace" value="iptv"/>
<parameter name="wsdlServiceElement" value="CSPResponseService"/>
<parameter name="schemaUnqualified" value="iptv"/>
<parameter name="wsdlServicePort" value="ctms"/>
<parameter name="className" value="com.example.webservice.c2.ctmsResp.CtmsSoapBindingSkeleton"/>
<parameter name="wsdlPortType" value="CSPResponse"/>
<parameter name="typeMappingVersion" value="1.2"/>
<parameter name="allowedMethods" value="*"/>
<typeMapping
xmlns:ns="iptv"
qname="ns:CSPResult"
type="java:com.example.webservice.c2.ctmsResp.CSPResult"
serializer="org.apache.axis.encoding.ser.BeanSerializerFactory"
deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
/>
</service>
<transport name="http">
<requestFlow>
<handler type="URLMapper"/>
<handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
</requestFlow>
<parameter name="qs:list" value="org.apache.axis.transport.http.QSListHandler"/>
<parameter name="qs:wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
<parameter name="qs.list" value="org.apache.axis.transport.http.QSListHandler"/>
<parameter name="qs.method" value="org.apache.axis.transport.http.QSMethodHandler"/>
<parameter name="qs:method" value="org.apache.axis.transport.http.QSMethodHandler"/>
<parameter name="qs.wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/>
</transport>
<transport name="local">
<responseFlow>
<handler type="LocalResponder"/>
</responseFlow>
</transport>
</deployment>
添加servlet过滤规则
新建com.example.servlet.WebServlet,继承AxisServlet。
package com.example.servlet;
import org.apache.axis.transport.http.AxisServlet;
@javax.servlet.annotation.WebServlet(
urlPatterns = "/services/*",
loadOnStartup = 1,
name = "AxisServlet"
)
public class WebServlet extends AxisServlet {
//无具体代码,使用注解的形式
}
重写Axis的配置工厂信息
- 如果要发布war到tomcat容器中,到此就结束了。但是由于本人想以jar包的形式发布,所以需要重写EngineConfigurationFactory类,否则访问不到。下面会进行详解。
新建org.apache.axis.configuration.EngineConfigurationFactoryServlet,继承EngineConfigurationFactoryDefault,将源代码copy过来,如下:
/*
* Copyright 2002-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.axis.configuration;
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;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import java.io.File;
import java.io.InputStream;
/**
* This is a default implementation of ServletEngineConfigurationFactory.
* It is user-overrideable by a system property without affecting
* the caller. If you decide to override it, use delegation if
* you want to inherit the behaviour of this class as using
* class extension will result in tight loops. That is, your
* class should implement EngineConfigurationFactory and keep
* an instance of this class in a member field and delegate
* methods to that instance when the default behaviour is
* required.
*
* @author Richard A. Sitze
* @author Davanum Srinivas (dims@apache.org)
*/
public class EngineConfigurationFactoryServlet
extends EngineConfigurationFactoryDefault
{
protected static Log log =
LogFactory.getLog(EngineConfigurationFactoryServlet.class.getName());
private ServletConfig cfg;
/**
* Creates and returns a new EngineConfigurationFactory.
* If a factory cannot be created, return 'null'.
*
* The factory may return non-NULL only if:
* - it knows what to do with the param (param instanceof ServletContext)
* - it can find it's configuration information
*
* @see org.apache.axis.configuration.EngineConfigurationFactoryFinder
*/
public static EngineConfigurationFactory newFactory(Object param) {
/**
* Default, let this one go through if we find a ServletContext.
*
* The REAL reason we are not trying to make any
* decision here is because it's impossible
* (without refactoring FileProvider) to determine
* if a *.wsdd file is present or not until the configuration
* is bound to an engine.
*
* FileProvider/EngineConfiguration pretend to be independent,
* but they are tightly bound to an engine instance...
*/
return (param instanceof ServletConfig)
? new EngineConfigurationFactoryServlet((ServletConfig)param)
: null;
}
/**
* Create the default engine configuration and detect whether the user
* has overridden this with their own.
*/
protected EngineConfigurationFactoryServlet(ServletConfig conf) {
super();
this.cfg = conf;
}
/**
* Get a default server engine configuration.
*
* @return a server EngineConfiguration
*/
public EngineConfiguration getServerEngineConfig() {
return getServerEngineConfig(cfg);
}
/**
* Get a default server engine configuration in a servlet environment.
*
* @param ctx a ServletContext
* @return a server EngineConfiguration
*/
private static
EngineConfiguration getServerEngineConfig(ServletConfig cfg) {
ServletContext ctx = cfg.getServletContext();
// Respect the system property setting for a different config file
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;
}
/**
* Flow can be confusing. Here is the logic:
* 1) Make all attempts to open resource IF it exists
* - If it exists as a file, open as file (r/w)
* - If not a file, it may still be accessable as a stream (r)
* (env will handle security checks).
* 2) If it doesn't exist, allow it to be opened/created
*
* Now, the way this is done below is:
* a) If the file does NOT exist, attempt to open as a stream (r)
* b) Open named file (opens existing file, creates if not avail).
*/
/*
* Use the WEB-INF directory
* (so the config files can't get snooped by a browser)
*/
String appWebInfPath = "/WEB-INF";
FileProvider config = null;
String realWebInfPath = ctx.getRealPath(appWebInfPath);
/**
* If path/file doesn't exist, it may still be accessible
* as a resource-stream (i.e. it may be packaged in a JAR
* or WAR file).
*/
if (realWebInfPath == null ||
!(new File(realWebInfPath, configFile)).exists())
{
String name = appWebInfPath + "/" + configFile;
InputStream is = ctx.getResourceAsStream(name);
if (is != null) {
// FileProvider assumes responsibility for 'is':
// do NOT call is.close().
config = new FileProvider(is);
}
if (config == null) {
log.error(Messages.getMessage("servletEngineWebInfError03",
name));
}
}
/**
* Couldn't get data OR file does exist.
* If we have a path, then attempt to either open
* the existing file, or create an (empty) file.
*/
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;
}
}
注意:我们需要修改的是getServerEngineConfig(ServletConfig cfg)方法,修改后的方法为:
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";
//由于部署方式变更为jar部署,此处不可以使用改方式获取路径
// ServletContext ctx = cfg.getServletContext();
// String realWebInfPath = ctx.getRealPath(appWebInfPath);
FileProvider config = null;
String realWebInfPath = EngineConfigurationFactoryServlet.class.getResource(appWebInfPath).getPath();
InputStream iss = ClassUtils.getResourceAsStream(EngineConfigurationFactoryServlet.class, appWebInfPath+"/" + SERVER_CONFIG_FILE);
if (iss != null) {
// FileProvider assumes responsibility for 'is':
// do NOT call is.close().
config = new FileProvider(iss);
}
if (config == null) {
log.error(Messages.getMessage("servletEngineWebInfError03", ""));
}
/**
* Couldn't get data OR file does exist.
* If we have a path, then attempt to either open
* the existing file, or create an (empty) file.
*/
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;
}
配置Application
添加注解 @ServletComponentScan //扫描自定义的WebFilter和WebListener,否则无法扫描到
启动程序,访问http://localhost:8080/services,会将server-config.wsdd里面的接口发布出来,此时已经可以调用了。
Demo链接
http://download.csdn.net/download/dong945221578/9894243
GitHub链接
注:由于下载量的问题,CSDN会自己动态调整积分下载,导致积分下载过高,且个人已经超过积分获取上限,所以样例在github上创建了一份。
https://github.com/donghc/demo