Spring4.x整合Axis1.4发布WebService服务

Spring4.x整合Axis1.4发布WebService服务

一、服务端部署

1. 在web.xml文件中添加映射路径和spring监听

<!-- webservices接口 axis 需要引入的 Servlet Start -->
	<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>
	<!-- webservices接口 axis 需要引入的 Servlet End -->
<!-- 新增spring容器配置 Start -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/config/spring-*.xml
		</param-value>
	</context-param>
	<!-- 新增spring容器配置 End -->

注:
1、spring监控如果不添加,服务发布正常,可以省略
2、文件中的扫描文件路径根据项目具体位置而定

2. 添加spring-axis.xml配置文件

  • 文件名自定义即可,作用是本地bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:context="http://www.springframework.org/schema/context"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
           				   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           				   http://www.springframework.org/schema/context
           				   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
   <bean   id="nFisCommonWebServiceImpl"   
           class="com.gblfy.service.impl.GblfyCommonWebServiceImpl" />  
</beans>

注:
1、 id默认是接口实现类的类名小写,也可以小写 自定义
2、 在web.xml配置文件中要配置spring监听

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           				http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           				http://www.springframework.org/schema/context
           				http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<bean id="webServiceImpl"
		class="com.gblfy.axis.service.impl.WebServiceImpl" />
</beans>

在这里插入图片描述

3. 添加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="adminPassword" value="admin" />
		<parameter name="sendXsiTypes" value="true" />
		<parameter name="sendMultiRefs" value="true" />
		<parameter name="sendXMLDeclaration" value="true" />
		<parameter name="axis.sendMinimizedElements" value="true" />
		<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="Authenticate"

		type="java:org.apache.axis.handlers.SimpleAuthenticationHandler" />
	<handler name="LocalResponder"

		type="java:org.apache.axis.transport.local.LocalResponder" />
	<handler name="URLMapper"
		type="java:org.apache.axis.handlers.http.URLMapper" />
	<service name="AdminService" provider="java:MSG">
		<parameter name="allowedMethods" value="AdminService" />
		<parameter name="enableRemoteAdmin" value="false" />
		<parameter name="className"
			value="org.apache.axis.utils.Admin" />
		<namespace>http://xml.apache.org/axis/wsdd/</namespace>
	</service>
	<service name="Version" provider="java:RPC">
		<parameter name="allowedMethods" value="getVersion" />
		<parameter name="className" value="org.apache.axis.Version" />
	</service>
	<transport name="http">
		<requestFlow>
			<handler type="URLMapper" />
			<handler
				type="java:org.apache.axis.handlers.http.HTTPAuthHandler" />
		</requestFlow>
	</transport>
	<transport name="local">
		<responseFlow>
			<handler type="LocalResponder" />
		</responseFlow>
	</transport>

	<!—北京接口服务  start-->
	<service name="GblfyCommonServiceShell" provider="java:RPC">
		<parameter name="allowedMethods" value="*" />
		<parameter name="className"
			value="com.gblfy.controller.webservice.GblfyCommonServiceShell" />
	</service>
	<!—北京接口服务  end-->
</deployment>

在这里插入图片描述
只修改截图部分即可!

4. 对外发布服务外壳类

  • 添加与server-config.wsdd配置文件,相对应对外发布服务外壳类
package com.gblfy.axis.controller;

import javax.xml.rpc.ServiceException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.remoting.jaxrpc.ServletEndpointSupport;

import com.gblfy.axis.service.IWebService;

public class WebServiceShell extends ServletEndpointSupport {
	
	@Autowired
	private ApplicationContext applicationContext;

	@Autowired
	private IWebService iWebService;

	// 注入bean
	@Override
	protected void onInit() throws ServiceException {
		// 初始化Spirng 配置
		applicationContext = super.getApplicationContext();
		iWebService = (IWebService) applicationContext.getBean("webServiceImpl");
	}

	public String webServiceForBJ(String tReqXml) throws Exception {
		return iWebService.webServiceForBJ(tReqXml);
	}
}

在这里插入图片描述

5. 添加接口类

package com.gblfy.axis.service;

public interface IWebService {
	/**
	 * 北京业务接口
	 * 
	 * @param xml
	 * @return
	 * @throws Exception
	 */
	public String webServiceForBJ(String tReqXml) throws Exception;
}

在这里插入图片描述

6. 添加接口逻辑实现类

package com.gblfy.axis.service.impl;

import com.gblfy.axis.service.IWebService;

public class WebServiceImpl implements IWebService {

	@Override
	public String webServiceForBJ(String tReqXml) throws Exception {

		return "接收到服务了!!!";
	}
}

在这里插入图片描述

7. 浏览器测试

效果图截图:
http://localhost:8081/spring-axis/services/WebServiceShell?wsdl
在这里插入图片描述

二、客户端部署

2.1 axis1.4 工具类封装(企业版本)

package com.gblfy.axis.utils;

import java.net.MalformedURLException;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.ibatis.javassist.tools.rmi.RemoteException;

/**
 * webService客户端
 */
public class WebServiceClientUtil {
	public static void main(String[] args) throws MalformedURLException, RemoteException, ServiceException, Exception {

		String url = "http://localhost:8081/spring-axis/services/WebServiceShell?wsdl";
		String namespace = "http://localhost:8081/spring-axis/services/WebServiceShell?wsdl";
		String method = "webServiceForBJ";
		String tReqXml = "客户端调用webservice服务方成功了!!!";
		WebServiceClientUtil a = new WebServiceClientUtil();
		String dataResponse = a.RequestToResponse(url, namespace, method, tReqXml);
		System.out.println("%%%%%%%%%$$$$$" + dataResponse);
	}

	/**
	 * 
	 * @param url       WebService地址
	 * @param namespace 命名空间
	 * @param method    方法
	 * @param tReqXml   请求报文
	 * @return resXml 响应报文
	 * @throws ServiceException
	 * @throws MalformedURLException
	 * @throws RemoteException
	 * @throws Exception
	 */
	public String RequestToResponse(String url, String namespace, String method, String tReqXml)
			throws ServiceException, MalformedURLException, RemoteException, Exception {

		Service service = new Service();
		Call call;
		String resXml = null;
		call = (Call) service.createCall();
		call.setTargetEndpointAddress(new java.net.URL(url));
		String subUrl = url.substring(url.lastIndexOf("/"));
		System.out.println("转发路径标志==" + subUrl.substring(1, subUrl.length()));
		// 针对北京业务添加判断,针对服务端有@Webservice 注解,有明确的参数因此,需要在客户端设置此设置
		// if判断 针对某个webservice服务 默认不走判断
		if ("BJServiceForClaim".equals(subUrl.substring(1, subUrl.length()))) {
			call.addParameter("xmlStr", org.apache.axis.Constants.XSD_STRING, ParameterMode.IN);
			call.setReturnType(org.apache.axis.Constants.XSD_STRING);
		}
		call.setOperationName(new QName(namespace, method));// 这是要调用的方法
		resXml = (String) call.invoke(new Object[] { tReqXml.toString() });
		return resXml;
	}

}

在这里插入图片描述
在这里插入图片描述

2.2 运行main方法测试

控制台输出

转发路径标志==WebServiceShell?wsdl
%%%%%%%%%$$$$$接收到服务了!!!

在这里插入图片描述

三、多接口服务发布

3.1 server-config.wsdd 添加服务

在这里插入图片描述

3.2 添加对外暴露的外壳类

3.3 添加服务接口

3.4 添加服务接口逻辑处理类

3.5 在spring-axis.xml 添加本地bean

在这里插入图片描述

3.6 发布服务

3.7 支持多个发布路径

在web.xml文件中

  • 默认 /services/*
  • 新增加/gblfy/services/*
    在这里插入图片描述

本文源码下载:

链接https://pan.baidu.com/s/1fVe3Fq_n4Ru9CHG_XCXVOg
提取码0iv1
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gblfy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值