java http webservice_在Spring中发布SOAP HTTP Webservice

这篇博客介绍了如何利用Spring和XFire在不依赖大型Servlet容器如Tomcat的情况下,创建并发布SOAP类型的WebService。通过编程接口创建XFire Service,并结合Jetty HTTP Server实现服务启动。同时,展示了如何通过Spring配置文件自动化发布多个POJO为Web Services,并监听Spring容器事件来启动和停止服务。
摘要由CSDN通过智能技术生成

(感觉BlogJava很有人气,就把原来写在msn space上的也转过来)

通常在Spring发布Hession,RMI等,是非常方便的,

但是要发布SOAP类型的WebService则要依赖一个独立的Servlet容器(如Tomcat+Axis),

这种Webservice一般还有别的配置文件,比如web.xml,wsdd文件等等

。有时侯,你可能想一台机器上只部署一个Http Soap Service

,这种场合你可能不希望安装一个类似Tomcat的容器,

你更希望发布的时候就是一个服务程序,该程序启动则提供WebService.这篇文章描述一种解决方案。

开发环境:

Spring 1.2.6

XFire 1.0

Jetty 4.2.1

方案描述:我们可以通过XFire的编程接口来创建WebService并嵌入一个HttpServer,

从而来做到在一个独立应用程序中发布Http Service1

4f1150b881333f12a311ae9ef34da474.png//Create an XFire Service24f1150b881333f12a311ae9ef34da474.pngObjectServiceFactory serviceFactory=newObjectServiceFactory();34f1150b881333f12a311ae9ef34da474.png        Service service=serviceFactory.create(Echo.class);44f1150b881333f12a311ae9ef34da474.png        service.setInvoker(newBeanInvoker(newEchoImpl()));54f1150b881333f12a311ae9ef34da474.png//Register the service in the ServiceRegistry64f1150b881333f12a311ae9ef34da474.pngXFire xfire=XFireFactory.newInstance().getXFire();74f1150b881333f12a311ae9ef34da474.png        xfire.getServiceRegistry().register(service);84f1150b881333f12a311ae9ef34da474.png//Start the HTTP server94f1150b881333f12a311ae9ef34da474.pngXFireHttpServer server=newXFireHttpServer();104f1150b881333f12a311ae9ef34da474.png        server.setPort(8191);114f1150b881333f12a311ae9ef34da474.png        server.start();

这样的话,如果发布多个WebSerice则要依次显式的创建 XFire Service,然后再一一注册,

这样显然是不够优雅的。

我们想要让开发者在Spring配置文件中指定要发布为WebService的POJOs,

然后载入Spring环境就自动发布为webservice,而不需要跟 XFire API打交道。

首先,我们想要一个BeanFacory,能把一个pojo装配成XFire Service1 /**2 *3 */4 packagecom.yovn.ws.xfire.example;5 6 importorg.codehaus.xfire.service.Service;7 importorg.codehaus.xfire.service.binding.BeanInvoker;8 importorg.codehaus.xfire.service.binding.ObjectServiceFactory;9 importorg.springframework.beans.factory.FactoryBean;10 11 /**12 *@authornew13 *14 */15 publicclassXFireServiceFactoryBeanimplementsFactoryBean16 {17 18 19 20 21 privateClass serviceClass;22 23 24 privateObject target;25 26 27 privateService service;28 29 30 privatefinalObjectServiceFactory sf=newObjectServiceFactory();31 32 /**33 *34 */35 publicXFireServiceFactoryBean()36 {37 38 }39 40 /*(non-Javadoc)41 * @see org.springframework.beans.factory.FactoryBean#getObject()42 */43 publicObject getObject()throwsException44 {45 if(service==null)46 {47 service=sf.create(serviceClass);48 service.setInvoker(newBeanInvoker(target));49 }50 returnservice;51 }52 53 /*(non-Javadoc)54 * @see org.springframework.beans.factory.FactoryBean#getObjectType()55 */56 publicClass getObjectType()57 {58 59 returnService.class;60 }61 62 /*(non-Javadoc)63 * @see org.springframework.beans.factory.FactoryBean#isSingleton()64 */65 publicbooleanisSingleton()66 {67 returntrue;68 }69 70 publicvoidsetServiceClass(Class serviceClass)71 {72 this.serviceClass=serviceClass;73 }74 75 publicvoidsetTarget(Object target)76 {77 this.target=target;78 }79 80 }81

这样我们可以通过Spring来装配一个pojo,

下一步我们要在Spring容器载入时候注册XFire Service,

并启动一个嵌入的Http Server,我们可以借助Spring的ApplicationListener来实现1 /**2 *3 */4 packagecom.yovn.ws.xfire.example;5 6 7 importorg.apache.commons.logging.Log;8 importorg.apache.commons.logging.LogFactory;9 importorg.codehaus.xfire.XFire;10 importorg.codehaus.xfire.XFireFactory;11 importorg.codehaus.xfire.server.http.XFireHttpServer;12 importorg.codehaus.xfire.service.Service;13 importorg.codehaus.xfire.service.ServiceRegistry;14 importorg.springframework.context.ApplicationContext;15 importorg.springframework.context.ApplicationEvent;16 importorg.springframework.context.ApplicationListener;17 importorg.springframework.context.event.ContextClosedEvent;18 importorg.springframework.context.event.ContextRefreshedEvent;19 20 /**21 *@authornew22 *23 */24 publicclassXFireServiceStarterimplementsApplicationListener25 {26 27 privateintport=80;28 29 privateXFireHttpServer server;30 privatefinalLog logger=LogFactory.getLog(getClass().getName());31 32 publicvoidsetPort(intport)33 {34 this.port=port;35 }36 37 publicvoidonApplicationEvent(ApplicationEvent event)38 {39 try40 {41 if(eventinstanceofContextRefreshedEvent)42 {43 44 if(server!=null)45 {46 47 server.stop();48 logger.info("xfire server stopped

9b8a8a44dd1c74ae49c20a7cd451974e.png");49 50 }51 registerService((ApplicationContext)event.getSource());52 server=newXFireHttpServer();53 server.setPort(port);54 server.start();55 logger.info("xfire server started

9b8a8a44dd1c74ae49c20a7cd451974e.png");56 57 }elseif(eventinstanceofContextClosedEvent)58 {59 if(server!=null)60 {61 server.stop();62 logger.info("xfire server stopped

9b8a8a44dd1c74ae49c20a7cd451974e.png");63 }64 65 }66 67 }catch(Exception e)68 {69 logger.error("process event"+event+"error",e);70 }71 72 }73 74 privatevoidregisterService(ApplicationContext context)75 {76 XFire xfire=XFireFactory.newInstance().getXFire();77 ServiceRegistry registry=xfire.getServiceRegistry();78 String names[]=context.getBeanNamesForType(Service.class);79 80 for(inti=0;i

1 packagecom.yovn.ws.xfire.example;2 3 publicclassAddImplimplementsAdd4 {5 6 publicintadd(inta,intb)7 {8 9 returna+b;10 }11 12 }

这是一个简单功能的POJO,下面我们在Spring中装配起来

1 <?xml  version="1.0" encoding="UTF-8"?>2 beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">3 4 5 6 7 8 9 10 11 12 13 

好了,我们完成了,只要载入这个xml初始化一个Spring ApplicationContext,一个名叫Add的webservice就发布了。你可以通过访问http://localhost/Add?wsdl来获得webservice的详细描述!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值