使用Spring的ServletEndpointSupport实现WebService接口范例
更新时间:2023-03-08 18:02:16 阅读量: 综合文库 文档下载
- 使用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 项目结构图:
正在阅读:
使用Spring的ServletEndpointSupport实现WebService接口范例03-08
主要材料价格表03-08
套扣式脚手架支撑方案 -10-23
美好时光作文600字07-07
华南理工大学先进班级体评选积分201003-08
亲近自然作文02-04
理化分析试样管理办法03-22
我要学会坚强02-07
全国中考数学压轴题(一)02-28
职业发展通道设计05-20
- 计算机试题
- 【2012天津卷高考满分作文】鱼心人不知
- 教育心理学历年真题及答案--浙江教师资格考试
- 20180327-第六届“中金所杯”全国大学生金融知识大赛参考题库
- 洪林兴达煤矿2018年度水情水害预测预报
- 基本要道讲义
- 机电设备安装试运行异常现象分析与对策
- 《有机化学》复习资料-李月明
- 非常可乐非常MC2--非常可乐广告策划提案 - 图文
- 2011中考数学真题解析4 - 科学记数法(含答案)
- 企业人力资源管理师三级07- 09年真题及答案
- 基于单片机的光控自动窗帘控制系统设计说明书1 - 图文
- 20160802神华九江输煤皮带机安装方案001
- (共53套)新人教版一生物必修2(全册)教案汇总 word打印版
- 2014行政管理学总复习
- 中国银监会关于加强地方政府融资平台贷款风险监管的指导意见
- 民宿酒店核心竞争与研究
- 游园活动谜语大全2012
- 河南省天一大联考2016届高三英语5月阶段性测试试题(六)(A卷)
- 小型超市管理系统毕业论文详细设计4
- ServletEndpointS
- 范例
- WebService
- 接口
- 实现
- 使用
- Spring
- upport
- 金华十校20172018学年第一学期调研考试
- 机械制造基础习题及答案
- 机电安装部分施工组织设计完整版 - 图文
- 基坑支护内支撑梁拆除施工方案
- 吕鑫个人简历
- (语文版)七年级下学期期末测试题
- 党的群众纪律是党的性质和宗旨的体现
- 全面解读中国各大银行机构,一文读懂银行
- 2台全角色Exchange 2013服务器3网卡NLB
- 电能质量供电电压允许波动和闪变
- 2-1 上海市高新技术成果转化项目认定申请书
- C语言程序设计试题复习题
- 厂务及生产常用缩写
- 七年级期末下册济南27中练习试题最新
- 八字推算运程的几种绝招 八字逢空断绝密真诀
- 观察学习理论
- 03G101-1勘误
- 2011光泽县驾校考试小型汽车试题
- 西南大学《数理统计》作业及答案
- 在农牧村过藏历新年