使用Spring的ServletEndpointSupport实现WebService接口范例
更新时间:2024-01-12 10:13:01 阅读量: 教育文库 文档下载
- 使用spring的好处推荐度:
- 相关推荐
使用Spring的ServletEndpointSupport实现WebService接口范例
一、环境配置 :在 eclipse 中配置引入相应的 Spring 框架( core/Remoting/Web )、 axis 包。
二、代码开发
1、 在 MyEclipse 中建立一个新的 J2EE 的 Web Project, 新建 java 包 test 。
2、 接口文件 HelloWorldRemote.java
package test;
//Spring 工程中要使用的接口文件 public interface HelloWorldRemote {
public String getMessage(String name); }
3、 接口实现文件 HelloWorldBean.java
package test;
//Spring 工程中要使用的接口实现文件
public class HelloWorldBean implements HelloWorldRemote {
private String helloStr; // Spring 中需要注入的字符串 public String getHelloStr() {
return helloStr; }
public void setHelloStr(String helloStr) {
this.helloStr = helloStr; }
// 实现接口中的方法
public String getMessage(String name) {
return helloStr + \ } }
4、 在 Spring 中对 Web Service 进行封装很简单,仅仅需要继承
org.springframework.remoting.jaxrpc.ServletEndpointSupport 类,实现里面的一些方法,包装一次,将其发布出来就可以。 HelloWorldWebService.java
package test;
import javax.xml.rpc.ServiceException;
import org.springframework.remoting.jaxrpc.ServletEndpointSupport; public
class
HelloWorldWebService
extends
ServletEndpointSupport
implements
HelloWorldRemote {
private HelloWorldRemote helloWorld; protected void onInit() throws ServiceException {
// 在 Spring 容器中获取 Bean 的实例
helloWorld = (HelloWorldRemote) getApplicationContext() .getBean(\ }
public String getMessage(String name) {
// 执行 Bean 中的相同的方法 return helloWorld.getMessage(name); } }
三、配置文件 (全部放在 /WEB-INF/ 目录下 )
1、 web.xml 为 web 加载 spring 和 axis 配置
org.springframework.web.context.ContextLoaderServlet
org.apache.axis.transport.http.AxisServlet
2、 applicationContext.xml 为 spring 的配置
3、 server-config.wsdd 为 axis 服务配置
xmlns:java=\ type=\ value=\
四、测试 客户端 TestWebServiceClient.java
package test;
import javax.xml.namespace.QName; import org.apache.axis.client.Call; import org.apache.axis.client.Service; public class TestWebServiceClient {
public static void main(String[] args) { try {
String wsdlUrl
= \ String nameSpaceUri
= \ // 创建调用对象
Service service = new Service(); Call call = null;
call = (Call) service.createCall(); // 调用 getMessage
System.out.println(\
call.setOperationName(new QName(nameSpaceUri, \ call.setTargetEndpointAddress(new java.net.URL(wsdlUrl)); String ret = (String) call.invoke(new Object[] { \ System.out.println(\ }
catch (Exception e) {
e.printStackTrace(); } } } 本
文
来
自
CSDN
博
客
,
转
载
请
标
明
出
处
:
http://blog.csdn.net/thinker28754/archive/2008/04/23/2318236.aspx
在spring中利用axis工具配置webservice成功案例
第一步:确认你用到的spring bean存在并且有效 比如 inviteService 第二步:创建要发布的服务方法 /** * */
package com.chinamobile.survey.webservice;
import java.util.List;
import org.springframework.remoting.jaxrpc.ServletEndpointSupport; import com.chinamobile.survey.entity.vo.ExamPlanContainer; import com.chinamobile.survey.invite.business.IInviteService; /**
* @author jianqiang.jiang * */
public class InviteEndPoint extends ServletEndpointSupport implements IInviteService {
// 将真实的业务bean包装成WebService private IInviteService inviteService;
// 该方法由Spring调用,将目标业务bean注入。 protected void onInit() {
this.inviteService = (IInviteService) getWebApplicationContext() .getBean(\ }
// 将业务bean的业务方法暴露成WebService
public int getAllowAnswerExamPlanCount(long userId) throws Exception { return inviteService.getAllowAnswerExamPlanCount(userId); }
public ExamPlanContainer getAllowAnswerExamPlanContainer(long userId, long offset, long maxRow) throws Exception {
return inviteService.getAllowAnswerExamPlanContainer(userId, offset, maxRow); }
}
注意里面的 ServletEndpointSupport 和对bean的引用 第三步.编辑两个文件 1.web.xml中 增加
org.apache.axis.transport.http.AxisServlet
2.生成server-config.wsdd,放在WEB-INF下
type=\ value=\ value=\/> languageSpecificType=\ainer\
注意其中的 返回类型注册, 第四步.
生成客户端测试代码:
package com.chinamobile.survey.webservice; import java.util.Iterator; import java.util.List;
import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType;
import org.apache.axis.encoding.ser.BeanDeserializerFactory; import org.apache.axis.encoding.ser.BeanSerializerFactory; import com.chinamobile.survey.entity.vo.ExamPlanContainer; import com.chinamobile.survey.entity.vo.WsExamPlan; public class ArchiveClient {
public static void main(String args[]) throws Exception { try {
String endpoint = \ Service service = new Service(); Call call = (Call) service.createCall();
QName searchresultqn = new QName(\ \
Class searchresultcls = ExamPlanContainer.class; call
.registerTypeMapping(searchresultcls, searchresultqn, new BeanSerializerFactory(searchresultcls, searchresultqn),
new BeanDeserializerFactory(searchresultcls, searchresultqn));
QName wsExamPlanQN = new QName(\ Class wsepcls = WsExamPlan.class;
call.registerTypeMapping(wsepcls, wsExamPlanQN,
new BeanSerializerFactory(wsepcls, wsExamPlanQN), new BeanDeserializerFactory(wsepcls, wsExamPlanQN)); call.setTargetEndpointAddress(new java.net.URL(endpoint)); call.setOperationName(new QName(\ \
call.addParameter(\ call.addParameter(\ call.addParameter(\ call.setReturnType(searchresultqn);
ExamPlanContainer result = (ExamPlanContainer) call .invoke(new Object[] { new Long(1), new Long(0), new Long(1000) }); if (result != null) {
List list = result.getExamPlanList();
for (Iterator iter = list.iterator(); iter.hasNext();) { WsExamPlan wsExamPlan = (WsExamPlan) iter.next(); System.out.println(wsExamPlan.getName()); } }
} catch (javax.xml.rpc.ServiceException e) { e.printStackTrace();
} catch (java.net.MalformedURLException e) {
e.printStackTrace();
} catch (java.rmi.RemoteException e) { e.printStackTrace(); } }
public static void testCount() { try {
String endpoint = \ Service service = new Service(); Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint)); call.setOperationName(new QName(\ \
call.addParameter(\ call.setReturnType(XMLType.XSD_INT); Integer result = (Integer) call
.invoke(new Object[] { new Long(1) }); System.out.println(result);
} catch (javax.xml.rpc.ServiceException e) { e.printStackTrace();
} catch (java.net.MalformedURLException e) { e.printStackTrace();
} catch (java.rmi.RemoteException e) { e.printStackTrace(); } } }
第五步:get方式访问测试
http://localhost:8080/exam/services/ExamService?method=getAllowAnswerExamPlanContainer¶mter0=1¶mter1=1¶mter2=1
建webservice服务的各种框架(axis、axis2、xifre) 文章分类:Java编程
此篇文章未完成,请稍后在查看。本来是保存为草稿的,可是又编辑几次后,不知道怎么就发布了。第一次写blog,大家别拍砖。。。
最近项目用了webservice,调研了几个框架(axis、axis2、xifre),现在分别来说明下这几个框架的使用及优缺点。
1、axis。
axis是这几个建webservice服务框架中,速度最慢、配置最复杂的一个,也难怪Apache要放弃它了。
首先在eclipse中建立一个web工程,取名为WebService-axis,建一个包test,在此包下建一个类Hello,Hello代码如下: Java代码
1. public class Hello {
2. public String sayHello(String name) { 3. return \
4. } 5. }
public class Hello {
右键点击这个类,“Web Services”——>“Create Web service”,出现如图:
然后点N下“Next”(其中有步要先点“start server”,要不“next”为灰不可用)。
完成之后,eclipse便帮我们将服务端和客户端都生成了,我们来看看都生成了哪些代码。 首先,服务端目录结构:
在集成Spring + Axis 的环境下webservice的发布和部署 2008-03-04 18:22 开发中遇到两个难点:
1. 在集成Spring + Axis 的环境下webservice的发布和部署; 2. 文件上传和下载的commons-fileupload-1.2的使用。 下面分别谈这两个问题.
一. 在集成Spring + Axis 的环境下webservice的发布和部署 1.1 DEMO文件样例 1.1.1 项目结构图:
(为了尊重原作者,我没有替换包名) 1.1.2 项目代码介绍 1.1.2.1 依次建立一下代码文件 查看评论1
------------------------------------------------------------------------------------ 基于以上文件,不用多做解释
1.1.3 用Myeclipse加入【Add Spring Capabilities……】
得到文件applicationContext.xml 并放在WEB-INF 下面。内容如下:
beans
PUBLIC
\
BEAN//EN\
\>
这点和原文的照本宣课可能不一样。我没有配置XXXX.service.xml文件 在该文件中注入一个bean的参数:【Say Hello to :】 用于输出的时候使用。 1.1.4 配置web.xml 文件如下: 请看评论
后段关于axis的部分在你发布wsdl的时候也会自动生成的,所以现帖出来;
在配置【org.springframework.web.context.ContextLoaderServlet】的上下文时,我们可以配置一个Servlet; 也可以做一个【listener】
org.springframework.web.context.ContextLoaderListener
1.1.5 利用wtp平台提供的自动话生成工具发布你的WebService 你的项目文档中将会产生以下几个文件夹或文件:
JaxRpcHelloWorldService server-config.wsdd wsdl
axRpcHelloWorld.wsdl
1.1.6 如果你在发布的时候启动了你的Tomcat 或WebLogic 那么你就会看见以下的画面 IE: = http://localhost:8088/HelloWorld_SpringAxisSample/services/JaxRpcHelloWorld
IE: = ?wsdlhttp://localhost:8088/HelloWorld_SpringAxisSample/services/JaxRpcHelloWorld
1.1.7 编写测试类
我的tomcat为8088 可能和你的不一样,请注意修改 1.1.8 运行测试类,结果如下:
正在阅读:
使用Spring的ServletEndpointSupport实现WebService接口范例01-12
租房合同安全协议书最新4篇03-23
冀教版三年级英语上册期末试卷及答案10-21
化工技术经济总复习提纲10-13
股权转让资产评估报告模板%26%2340%3B最新整理)05-05
数据库保护习题及答案10-27
2013.4.25计划免疫宣传日实施方案10-04
基础知识考试试卷-水利06-12
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- ServletEndpointS
- 范例
- WebService
- 接口
- 实现
- 使用
- Spring
- upport
- 2013年上半年汽车工业经济运行情况
- 三年级古诗竞赛试题
- 公司2011年上半年安全生产情况通报
- 学术写作范文17篇 - 图文
- 七世达赖喇嘛噶桑嘉措生平研究
- 管线及管道协调会议纪要(4.14)
- 信号波形合成实验电路-毕业设计 - 图文
- 2018届高考考前适应性试卷(一)(文综)
- Atfbrla - a2010年西南政法大学招收硕士学位研究生考试参考书目
- 四年级上语文教学反思-太阳,你好!-语文A版 小学学科网
- 护理专业护士专业知识模拟题 二、三
- 中小学教师队伍建设汇报材料 6.17
- 2015-2016年下学期初一信息技术教师工作总结
- 糖尿病与肥胖症的防治(你好名老中医堂)
- 2014年福建省信息技术高考总复习试题(2) - 图文
- 计算机基础实训任务书 - 图文
- GMAT曼哈顿中文版总结 - 图文
- 2018年版网络二级建造师继续教育建筑基坑工程安全管理规范
- LED可调驱动电路电源设计
- 关于证券公司开展集合资产管理业务有关问题的通知