Spring4.x整合Axis1.4发布WebService服务
文章目录
- 1. 在web.xml文件中添加映射路径和spring监听
- 2. 添加spring-axis.xml配置文件
- 3. 添加server-config.wsdd配置文件
- 4. 对外发布服务外壳类
- 5. 添加接口类
- 6. 添加接口逻辑实现类
- 7. 浏览器测试
- 3.1 server-config.wsdd 添加服务
- 3.2 添加对外暴露的外壳类
- 3.3 添加服务接口
- 3.4 添加服务接口逻辑处理类
- 3.5 在spring-axis.xml 添加本地bean
- 3.6 发布服务
- 3.7 支持多个发布路径
- 本文源码下载:
一、服务端部署
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/*
本文源码下载:
链接 | |
提取码 | 0iv1 |