webservice-之使用axis+spring开发_第1页
webservice-之使用axis+spring开发_第2页
webservice-之使用axis+spring开发_第3页
webservice-之使用axis+spring开发_第4页
webservice-之使用axis+spring开发_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、webservice之使用axis+spring开发收藏一、环境配置 :在 eclipse 中配置引入相应的 Spring 框架( core/Remoting/Web )、 axis 包。 二、代码开发1、 在 MyEclipse 中建立一个新的 J2EE 的 Web Project, 新建 java 包 test 。2、 接口文件 HelloWorldRemote.javapackage test;/Spring 工程中要使用的接口文件public interface HelloWorldRemotepublic String getMessage(String name);3、 接口实现文

2、件 HelloWorldBean.javapackage test;/Spring 工程中要使用的接口实现文件public class HelloWorldBean implements HelloWorldRemoteprivate String helloStr; / Spring 中需要注入的字符串public String getHelloStr()return helloStr;public void setHelloStr(String helloStr)this.helloStr = helloStr;/ 实现接口中的方法public String getMessage(Stri

3、ng name)return helloStr + : + name;4、 在 Spring 中对 Web Service 进行封装很简单,仅仅需要继承 org.springframework.remoting.jaxrpc.ServletEndpointSupport 类,实现里面的一些方法, 包装一 次,将其发布出来就可以。 HelloWorldWebService.javapackage test;import javax.xml.rpc.ServiceException;import org.springframework.remoting.jaxrpc.ServletEndpoint

4、Support;public class HelloWorldWebService extends ServletEndpointSupport implementsHelloWorldRemoteprivate HelloWorldRemote helloWorld;protected void onInit() throws ServiceException/ 在 Spring 容器中获取 Bean 的实例helloWorld = (HelloWorldRemote) getApplicationContext().getBean(myHelloWorldBean);public Stri

5、ng getMessage(String name)/ 执行 Bean 中的相同的方法 return helloWorld.getMessage(name);三、配置文件 (全部放在 /WEB-INF/ 目录下 )1、web.xml 为 web 加载 spring 和 axis 配置 contextConfigLocation /WEB-INF/applicationContext.xmlcontext org.springframework.web.context.ContextLoaderServlet 1axis org.apache.axis.transport.http.AxisSe

6、rvlet 2axis/services/*2、applicationContext.xml 为 spring 的配置Say Hello to :3、server-config.wsdd为 axis 服务配置 /axis/wsdd/ 四、测试 客户端 TestWebServiceClient.javapackage test;import space.QName;import org.apache.axis.client.Call;import org.apache.axis.client.Service;public cla

7、ss TestWebServiceClientpublic static void main(String args)tryStri ng wsdIUrl=http:/localhost:8080/spri ng-axis/services/myWebService?wsdl;Stri ng n ameSpaceUri=http:/localhost:8080/spri ng-axis/services/myWebService;/创建调用对象Service service = new Service。;Call call = n ull;call = (Call) service.creat

8、eCall();/ 调用 getMessageSystem.out.pri ntl n(getMessage);call.setOperati onN ame( new QName( nameSpaceUri, getMessage); call.setTargetEndpointAddress(new .URL(wsdlUrl);String ret = (String) call.invoke(new Object ABC ); System.out.pri ntl n(retur n value is + ret);catch (Excepti on e)e.pri nt

9、StackTrace();本文来 自 CSDN 博客, 转载请标明 出处http:/blog.csd n.n et/thi nker28754/archive/2008/04/23/2318236.aspx在spring 中利用 axis 工具配置 webservice成功案例*/ package com.chi namobile.survey.webservice;import java.util.List;import org.spri ngframework.remoti ng.jaxrpc.ServletE ndpoi ntSupport;import com.chi namobile

10、.survey.e ntity.vo.ExamPla nCon tai ner;import com.chi namobile.survey.i nvite.busi ness.ll nviteService;/* author jia nqian g.jia ng*/public class In viteE ndPoi nt exte nds ServletE ndpoi ntSupport impleme ntsII nviteService / 将真实的业务bean包装成WebServiceprivate llnviteService inviteService;/该方法由Spring

11、调用,将目标业务bean注入。protected void onln it() this.i nviteService = (ll nviteService) getWebApplicatio nCon text().getBea n(i nviteService);/将业务bean的业务方法暴露成WebServicepublic int getAllowA nswerExamPla nCoun t(lo ng userld) throws Excepti on return in viteService.getAllowA nswerExamPla nCoun t(userld);publi

12、c ExamPla nContainer getAllowA nswerExamPla nContain er(l ong userld, l ongoffset, l ong maxRow)throws Excepti on return in viteService.getAllowA nswerExamPla nCon tai ner(userld, offset, maxRow);注意里面的ServletEndpointSupport 和对bean的引用第三步.编辑两个文件l.web.xml 中增加vservlet -n ameAxisServletorg.apache.axis.tr

13、a nsport.http.AxisServletAxisServlet/services/*v/servlet-mapp ing2.生成 server-config.wsdd,放在 WEB-INF 下vdeployme nt xml ns=/axis/wsdd/xml ns:java=/axis/wsdd/providers/java vservice n ame=ExamService provider=java:RPC vparameter n ame=classNamevalue=com.ch in amo

14、bile.survey.webservicen viteE ndPo in t / vparameter n ame=allowedMethodsvalue=getAllowA nswerExamPla nCoun t,getAllowA nswerExamPla nContainer/vparameter n ame=allowedRoles value=user1,user2 /注意其中的 返回类型注册,第四步.生成客户端测试代码:package com.chi namobile.survey.webservice;import java.util.Iterator;import java

15、.util.List;import javax.xml. namespace.QName;import javax.xml.rpc.ParameterMode;import org.apache.axis.clie nt.Call;import org.apache.axis.clie nt.Service;import org.apache.axis.e ncodi ng.XMLType;import org.apache.axis.e ncodi ng.ser.Bea nDeserializerFactory;import org.apache.axis.e ncodi ng.ser.Be

16、a nSerializerFactory;import com.chi namobile.survey.e ntity.vo.ExamPla nCon tai ner;import com.chi namobile.survey.e ntity.vo.WsExamPla n;public class ArchiveClie nt public static void main(String args) throws Exception try String en dpo int = http:/localhost:8080/exam/services/ExamService;Service s

17、ervice = new Service();Call call = (Call) service.createCall();QName searchresult qn = new QName(urn:Bea nService,ExamPla nCo ntai ner);Class searchresultcls = ExamPla nCon tai ner.class;call.registerTypeMapp in g(searchresultcls, searchresult qn, new Bea nSerializerFactory(searchresultcls, searchre

18、sult qn),new Bea nDeserializerFactory(searchresultcls,searchresult qn);QName wsExamPla nQN = new QName(urn:Bea nService, WsExamPla n);Class wsepcls = WsExamPla n.class;call.registerTypeMapp in g(wsepcls, wsExamPla nQN,new Bea nSerializerFactory(wsepcls, wsExamPla nQN),new Bea nDeserializerFactory(ws

19、epcls, wsExamPla nQN);call.setTargetEndpointAddress(new .URL(endpoint); call.setOperati onN ame( new QName(ExamService,getAllowA nswerExamPla nContain er);call.addParameter(userld, XMLType.XSD_LONG, ParameterMode.IN);call.addParameter(offset, XMLType.XSD_LONG, ParameterMode.IN);call.addParam

20、eter(maxRow, XMLType.XSD_LONG, ParameterMode.IN);call.setRetur nType(searchresult qn);ExamPla nContainer result = (ExamPla nContainer) call.inv oke( new Object new Lon g(1), new Lon g(0),new Lon g(1000) );if (result != n ull) List list = result.getExamPla nList();for (Iterator iter = list.iterator()

21、; iter.hasNext();) WsExamPla n wsExamPla n = (WsExamPla n) iter. next();System.out.pri ntl n( wsExamPla n.getName(); catch (javax.xml.rpc.ServiceExcepti on e) e.pri ntStackTrace(); catch (java .n et.MalformedURLExcepti on e) e.pri ntStackTrace(); catch (java.rmi.RemoteExceptio n e) e.pri ntStackTrac

22、e();public static void testCo un t() try String en dpo int = http:/localhost:8080/exam/services/ExamService;Service service = new Service();Call call = (Call) service.createCall();call.setTargetEndpointAddress(new .URL(endpoint); call.setOperati onN ame( new QName(ExamService,getAllowA nswer

23、ExamPla nCou nt);call.addParameter(userld, XMLType.XSD_LONG, ParameterMode.IN); call.setReturnType(XMLType.XSD _IN T);In teger result = (In teger) call.inv oke( new Object new Lon g(1) );System.out.pri ntln( result); catch (javax.xml.rpc.ServiceExcepti on e) e.pri ntStackTrace(); catch (java .n et.M

24、alformedURLExcepti on e) e.pri ntStackTrace(); catch (java.rmi.RemoteExceptio n e) e.pri ntStackTrace();第五步:get方式访问测试http:/localhost:8080/exam/services/ExamService?method=getAllowA nswerExamPla nContain er¶mter0=1 ¶mter1=1 ¶mter2=1建 webservice服服务的各种框架(axis、axis2、xifre)文章分类:Java编程此篇文章未完成,

25、请稍后在查看。本来是保存为草稿的,可是又编辑几次后,不知道怎么就 发布了。第一次写blog,大家别拍砖。最近项目用了 webservice,调研了几个框架(axis、axis2、xifre),现在分别来说明下这几个 框架的使用及优缺点。1、axis。axis是这几个建webservice服务框架中,速度最慢、配置最复杂的一个,也难怪Apache要放弃它了。首先在eclipse中建立一个 web工程,取名为 WebService-axis建一个包test,在此包下建一 个类Hello, Hello代码如下:Java代码Tj1. public class Hello 2. public Stri

26、ng sayHello(Stri ng n ame) 3. return Hello, + n ame;4. 5. public class Hello 右键点击这个类,“Web Services ” “Create Web se,ce现如图:然后点N下“Next”(其中有步要先点“start serve, 要不 “next为灰不可用)。完成之后,eclipse便帮我们将服务端和客户端都生成了,我们来看看都生成了哪些代码首先,服务端目录结构:-WelbService-flMi e-src-田 test+M&llo. java4 aft JUZ System Library j dkl DD+

27、W. Apach& Tomcat v6.0 Apach& Tomcat v6.0+ 皂 H电b App Libi世i 电m-p build-123 ebContent+ 口 M1ETA-INF-& WEB-IHF-& KtllcStrvi c-l严 test(xj deploy, wsdd deploy, wsdd. b&k基 undeploy, sdd+ LzW libmJ server-coni g. wsdd-i2 wsdlHello, wsdl在集成Spring + Axis的环境下webservice的发布和部署2008-03-04 18:22开发中遇到两个难点:1. 在集成Spri

28、ng + Axis的环境下webservice的发布和部署;2. 文件上传和下载的 commons-fileupload-1.2的使用。下面分别谈这两个问题一.在集成Spring + Axis的环境下webservice的发布和部署1.1 DEMO文件样例1.1.1 项目结构图: st? ma r bsi rn白STU9 曲匕 wr*+ IHellarli jtvi-) tf- com. test. httk. iBpl+ 丄 HllQlvrldlBpl Jftni f- 出 cah. wn. webrvi cet- r-T.,;+ J JaxIipMellffurld java+ 為;JTE

29、 Ssttrs Li br r/ jiicl. S. 0:+Aacht Tttcat v5. & Aptch T弼mt v5.5+ r- tf*b App Lilrhriti+ 送,bwlii- : J- WtbCAtnl+; f MjrT/-IHT-WEB-IWF+/ - JaKRpcHel!心MfliSe E呎匚 E+ & lit力 applic&tionCoaLteEt.xmlTC Eerier-cemfi.*rE(H*J wtb.6 fe wiH-XeMail oW wri d. us JI=田 Tr Servers(为了尊重原作者,我没有替换包名)1.1.2项目代码介绍1.121依次

30、建立一下代码文件查看评论1基于以上文件,不用多做解释1.1.3 用 Myeclipse 加入【Add Spring Cap abilities】Ruin AsDebug AsProfile AsValidateW, Update EAR Librari esTeamConp4r e tti thBeEtore from Local Hi stor. P.MyEclipseAdd ad Remove Fr&ject DeplojniiiitE,Kdd Sprinng Capabilities-.Add VbProj act Cap&bili ti es. d hAdd HibemaCapabil

31、i ti es.Bun IDocletDp丑 in ExplorerRun Vali dati on得到文件applicationContext.xml并放在WEB-INF下面。内容如下:v?xml version=1.0 encoding=UTF-8?vproperty name=helloStr Say Hello to :v/bea ns这点和原文的照本宣课可能不一样。我没有配置XXXX.service.xml文件在该文件中注入一个bean的参数:【Say Hello to :用于输出的时候使用。1.1.4 配置web.xml文件如下:请看评论后段关于axis的部分在你发布wsdl的时候也会自动生成的,所以现帖出来;在配置【org.springframework.web.context.ContextLoaderServle的上下文时,我们可以配置一 个Servlet;也可以做一个【listenervliste nervliste ner-classorg.spri ngframework.web.co ntext.Co n

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论