Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > Webservices with Spring, XFire and jsr181
【标  题】:Webservices with Spring, XFire and jsr181
【关键字】:Webservices,with,Spring,XFire,and,jsr181
【来  源】:http://blog.csdn.net/mengxuwq/archive/2007/03/05/1520801.aspx

Webservices with Spring, XFire and jsr181

Your Ad Here
Just wanted to share my experiences i made yesterday while implementing webservices via XFire. Since i migrated to Java5 yesterday, i thought its a good idea to use its strengths in form of using annotations for webservices. Because i am using Spring, XFire was an obvious choice because of its neat integration into Spring. So take a seat and see yourself how easy it is to create webservices with this combination.

 

I will NOT talk about bootstrapping a Spring context and stuff, so lets start with writing an application Context for XFire.

First lets focus on the web.xml:

<context-param>  <param-name>contextConfigLocation</param-name>  <param-value>/WEB-INF/conf/spring/applicationContext.xml,            /WEB-INF/conf/spring/xfire-servlet.xml  </param-value></context-param>

<servlet> <servlet-name>XFireServlet</servlet-name> <servlet-class> org.codehaus.xfire.spring.XFireSpringServlet </servlet-class></servlet>

<servlet-mapping> <servlet-name>XFireServlet</servlet-name> <url-pattern>/servlet/XFireServlet/*</url-pattern></servlet-mapping>

<servlet-mapping> <servlet-name>XFireServlet</servlet-name> <url-pattern>/services/*</url-pattern></servlet-mapping>

The "contextConfigLocation" tells Spring where to find the applicationContext XML files. applicationContext.xml was there before and i just added an XFire one. Of course you can put the stuff below also in the applicationContext.xml but i like to seperate things. In fact, in reality i have a lot more XMLs in there, but i left them out for this example.

After that you only have to define the XFireServlet which handles the requests that comes to the URLs specified in "url-pattern". You are free to change this of course.

Lets look at the Spring applicationContext for XFire called xfire-servlet.xml

 

<beans> <bean id="webAnnotations"      class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations"/> <bean id="handlerMapping"      class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping">        <property name="typeMappingRegistry">            <ref bean="xfire.typeMappingRegistry"/>        </property>        <property name="xfire">            <ref bean="xfire"/>        </property>        <property name="webAnnotations">            <ref bean="webAnnotations"/>        </property> </bean>

<bean id="annotatedHello" class="de.logentis.netversys.domain.webservice.HelloImpl"/>

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/"> <ref bean="handlerMapping"/> </entry> </map> </property> </bean>

<import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/></beans>

This basically defines the handler for the http requests and imports some other xfire stuff. All this is one-time plumbing. After that you only define your webservice POJOs like seen with "annotatedHello". You only define your implementation webservice POJO in here and thats it. No more magic needed.

Be aware of the fact that xfire.xml which gets imported uses its own PropertyEditor called "customEditorConfigurer". Because this is a very common name in Spring for defining your own editors and the lack of namespaces in Spring 1.x, it could be that your own bean with the same name gets overridden, so be aware of that and use a different name if you have customEditors. I ran into this...

Now lets take a look at the webservice POJO:

 

import javax.jws.WebService;

@WebService( serviceName = "HelloService", endpointInterface = "Hello")public class HelloImpl implements Hello{

public String world() { return "hello World!"; }

public String world2() { return "my name is slim shady"; }

public Person world3() { Person o = new Person("Foo", 21); o.setAddress(new Address("fooStreet", "12", "New York")); return o; }

}

This is really simple, we only define the @WebService annotation with some parameters like webservice name as its used on client side and tell the jsr181 processor that there is an interface to export the webservice methods.

You dont need to have an interface for it but its a better programming style, so we create also an interface. If you dont want to do it, you can put all the other annotations also in the implementation class.

 

package de.logentis.netversys.domain.webservice;

import javax.jws.WebService;import javax.jws.WebResult;

@WebServicepublic interface Hello {

public String world();

@WebResult(name="slimName") public String world2();

@WebResult(name="MyObject") public Person world3();}

In the interface we dont need to define the @WebMethod annotation, because all methods in the interface get exported. Things like @WebResult are totally optional and let you control how the WSDL looks like. You can leave it out without any problems. There are in fact a lot more annotations to shape the WSDL to non-default values.

And what more? Nothing. Thats it. Deploy and you can test your webservice by using XFire as well or by just using <a href="http://www.soapui.org/">SOAPUI</a>.

In case you want to use XFire for testing, here the is the client code:

 

import org.codehaus.xfire.service.Service;import org.codehaus.xfire.service.binding.ObjectServiceFactory;import org.codehaus.xfire.client.XFireProxyFactory;import org.codehaus.xfire.annotations.AnnotationServiceFactory;import de.logentis.netversys.domain.webservice.Hello;import de.logentis.netversys.domain.webservice.HelloImpl;import de.logentis.netversys.domain.webservice.Person;

import java.net.MalformedURLException;public class TestRunner {

public static void main(String[] args) { Service serviceModel = new AnnotationServiceFactory().create( HelloImpl.class);

try { Hello service = (Hello)new XFireProxyFactory().create( serviceModel, "http://localhost:8080/mycontext/services/HelloService"); String s = service.world(); System.out.println(s); } catch (MalformedURLException e) { e.printStackTrace(); }

}}

Remember, if you copy and paste, you need at least change the "mycontext" to your current application context.

 
Vector、ArrayList和hashtable hashmap的异同:【上一篇】
Web文件的ContentType类型大全:【下一篇】
【相关文章】
  • Xfire and JSR 181 Annotations
  • Spring JDBC抽象框架简化Web数据库开发
  • Spring的远程服务调用
  • Spring2.0新特性几相关点评
  • 常量和指针(Pointers and Constants)
  • Tomcat 6 released and stable
  • On-line Table Reorganization and Redefinition
  • SAN Extension and Routing
  • sed tricks and pitfalls
  • SpringSide开发实战(六):AJAX,在地狱中漫步
  • 【随机文章】
  • 祛魅《把信送给加西亚》
  • 铁齿铜牙纪晓岚精妙语收藏
  • perl 5基础教程——数据类型
  • IBM 刀片群集(XCAT)的安装与性能测试
  • XSL基础教程(一)
  • iscsymf ( )【C语言库函数源代码】
  • resin优化经验
  • SQL Server 连接中四种最常见错误
  • 计算机开机故障详解(转)
  • 制作网页常用代码
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.