使用Axis开发Web Service程序整合 spring
来源:互联网 发布:软件企业认定 招标 编辑:程序博客网 时间:2024/04/30 06:37
使用Axis开发Web Service程序整合 spring
其中项目需要必要的 axis包和 spring 包
1、编写服务端程序接口
package com.xino.iservice;
public interface ISayHello {
//接口
public String getName(String name);
}
编写接口的实现:
package com.xino.serviceimpl;
import com.xino.iservice.ISayHello;
public class SayHello implements ISayHello{
public String getName(String name)
{
return "hello "+name;
}
}
编写 spring实现
/**
*
*/
package com.xino.spring;
import javax.xml.rpc.ServiceException;
import org.springframework.remoting.jaxrpc.ServletEndpointSupport;
import com.xino.iservice.ISayHello;
/**
* @author Administrator
*需要继承 ServletEndpointSupport类
*/
public class SpringRpcWebServices extends ServletEndpointSupport implements
ISayHello {
private ISayHello sayHello;
protected void onInit() throws ServiceException {
// 在Spring容器中获取Bean的实例
sayHello = (ISayHello) getApplicationContext()
.getBean("testSiService");
}
/* (non-Javadoc)
* @see com.xino.iservice.ISayHello#getName(java.lang.String)
*/
public String getName(String name) {
// TODO Auto-generated method stub
return sayHello.getName(name);
}
}
2、编写wsdd文件
deploy.wsdd文件内容如下:SayHello服务名 com.xino.spring.SpringRpcWebServices提供服务的包和类
<parameter name="allowedMethods" value="*"/> 这里 value="*"表示提供的所有函数,也可以直接写函数名如 <parameter name="allowedMethods" value="getName,getTwoName"/> 多个函数名用逗号 “,”
格开这里是两个函数,分别是 getName和 getTwoName
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="SayHello" provider="java:RPC">
<parameter name="className" value="com.xino.spring.SpringRpcWebServices"/>
<parameter name="allowedMethods" value="*"/>
</service>
</deployment>
3、发布服务:
编辑一个deploy.bat,Axis_Lib为axis.jar路径。本机为 ./lib目录内容如下:AxisWebservice为项目名称
set Axis_Lib=./lib
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
set Axis_Servlet=http://localhost:8080/AxisWebservice/servlet/AxisServlet
%Java_Cmd% org.apache.axis.client.AdminClient -l%Axis_Servlet% deploy.wsdd
执行这个批处理文件,这时候,如果提示成功的话,访问http://localhost:8080/AxisWebservice/services就会显示服务列表。其中 AxisWebservice为项目名称
如下:
And now... Some Services
AdminService (wsdl)
AdminService
Version (wsdl)
getVersion
//下面就是我们刚才的服务函数了
SayHello (wsdl)
getName
4. 编辑web.xml文件内容 其中, web.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Apache-Axis</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/TestServer.xml</param-value>
</context-param>
<!-- -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.apache.axis.transport.http.AxisHTTPSessionListener
</listener-class>
</listener>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>
org.apache.axis.transport.http.AxisServlet
</servlet-class>
</servlet>
<!--
<servlet>
<servlet-name>AdminServlet</servlet-name>
<servlet-class>
org.apache.axis.transport.http.AdminServlet
</servlet-class>
<load-on-startup>100</load-on-startup>
</servlet>
<servlet>
<servlet-name>SOAPMonitorService</servlet-name>
<servlet-class>
org.apache.axis.monitor.SOAPMonitorService
</servlet-class>
<init-param>
<param-name>SOAPMonitorPort</param-name>
<param-value>5001</param-value>
</init-param>
<load-on-startup>100</load-on-startup>
</servlet>
-->
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/servlet/AxisServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>*.jws</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<!--
<servlet-mapping>
<servlet-name>SOAPMonitorService</servlet-name>
<url-pattern>/SOAPMonitor</url-pattern>
</servlet-mapping>
-->
<mime-mapping>
<extension>wsdl</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>
<mime-mapping>
<extension>xsd</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
××××××××××××××××××××××××××××
/WEB-INF/conf 目录下增加 TestServer.xml spring配置文件
TestServer.xml 文件内容为
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 装载属性配置文件-->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>WEB-INF/classes/config.properties</value>
</list>
</property>
</bean>
<!-- 对外发布服务 -->
<bean id="testSiService" class="com.xino.serviceimpl.SayHello">
</bean>
</beans>
在 src目录下增加配置文件 config.properties内容为
#对方webservices地址格式:http://IP:端口/项目名称/services/服务名称
WS_URL=http://127.0.0.1:8080/AxisWebservice/services/SayHello
#ADC支持厂商(微软(MS),华为(HW)等等)
ADC_SUPPORT=HW
#地区编码(ADC平台部署在哪个省)
AREACODE=0591
#业务应用标识(业务代码)
SIAPPID=10000
#企业code
SICODE=9001
#密钥
KEY=001
#供销通地址
GXT_URL=http://192.168.1.188:8080
#正确代码
TRUE_CODE=
#错误代码
FALSE_CODE=
其中最重要的就是 WS_URL=http://127.0.0.1:8080/AxisWebservice/services/SayHello这句,为测试所用
5、生成客户端辅助工具文件
package com.xino.core;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class CommUtil {
/**
* 获取配置文件信息
* @param key
* @return
*/
public String getProperty(String key) {
System.out.println("获取配置文件中相关信息");
Properties p = new Properties();
InputStream in = this.getClass().getResourceAsStream("/config.properties");
try {
p.load(in);
System.out.println("获取到的配置文件中的信息" + key + "为:=" + p.getProperty(key));
return p.getProperty(key);
} catch (IOException e) {
System.out.println("获取配置文件错误"+e.getMessage());
}
return null;
}
}
package com.xino.core;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class UtilToolTest {
/**
* 调用对方webservices
* @param object
* @return
*/
public static Call callWebServices(String method) {
System.out.println("调用方法"+ method);
try {
String nameSpaceUri = new CommUtil().getProperty("WS_URL");
// 结果例子http://localhost:8080/ADCProject/services/SIInterfaceForADC;
String wsdlUrl = nameSpaceUri + "?wsdl";
//结果例子 http://localhost:8080/ADCProject/services/SIInterfaceForADC?wsdl;
//创建调用对象
Service service = new Service();
Call call = null;
call = (Call) service.createCall();
// 调用方法
call.setOperationName(new QName(nameSpaceUri, method));
call.setTargetEndpointAddress(new String(wsdlUrl));
//call.invoke(new Object[] { "" });
return call;
} catch (Exception e) {
System.out.println("调用webservices错误:" + e.getMessage());
}
return null;
}
}
6、编写客户端程序,编译并执行
下面是一段junit测试客户端代码。
package com.xino.spring.client;
import java.rmi.RemoteException;
import junit.framework.TestCase;
import org.apache.axis.client.Call;
import com.xino.core.UtilToolTest;
public class TestHello extends TestCase{
public void testHello() {
//参数是要调用的方法名称
Call call = UtilToolTest.callWebServices("getName");
if(null == call) {
System.out.println("调用webservices错误;");
return;
}
try {
//这里是真正调用方法
String str = (String)call.invoke(new Object[]{"测试"});
System.out.println("调用webservices得到的结果为:"+str);
} catch (RemoteException e) {
System.out.println("调用webservices错误:"+e.getMessage());
}
}
}
至此,整个服务器端和客户端的Web Service框架代码就完成了。
执行效果如下:
调用方法getName
获取配置文件中相关信息
获取到的配置文件中的信息WS_URL为:=http://127.0.0.1:8080/AxisWebservice/services/SayHello
调用webservices得到的结果为:hello测试
- 使用Axis开发Web Service程序 整合 spring
- 使用Axis开发Web Service程序整合 spring
- 使用Axis开发Web Service程序
- 使用Axis开发Web Service程序
- 使用Axis开发Web Service程序
- 使用Axis开发Web Service程序
- 使用Axis开发Web Service程序
- 使用Axis开发Web Service程序
- 使用Axis开发Web Service程序
- 使用Axis开发Web Service程序
- 使用Axis开发Web Service程序
- 使用Axis开发Web Service程序
- 使用Axis开发Web Service程序
- 使用Axis开发Web Service程序
- 使用Axis开发Web Service程序
- 使用Axis开发Web Service程序
- 使用Axis开发Web Service程序
- 使用Axis开发Web Service程序
- HDOJ3887-DFS人工栈,树状数组
- 代码规范的那些事儿
- 关于STL中的pop操作为什么没有返回值
- 去掉UCenter验证码的修改方法
- jQuery插件 -- 动态事件绑定插件jquery.livequery.js
- 使用Axis开发Web Service程序整合 spring
- SYN洪水攻击
- HDOJ1853-费用流(圈,拆点)
- 深入了解php底层机制
- 下载图片(文件)到SD文件
- Quartz 配置实例
- AXIS和spring的远程服务整合的应用
- static作用(修饰函数、局部变量、全局变量)
- AT88SC0104C加密原理