REST架构之Apache Wink介绍

来源:互联网 发布:域名top 编辑:程序博客网 时间:2024/06/06 20:31

     REST(Representational State Transfer)  based Web Service是相对于传统的Web Service(SOAP+WSDL+UDDI)而提出的。传统的Web Service可以很好的解决异构系统之间的通信问题,但是需要首先定义好XML格式的合同(WSDL),client和server都必须严格遵守协议,不容易升级以及集群伸缩。REST Web Service不需要事先定义格式,传输的内容也可以依据不同的client变化(json,xml,html等),最重要的是使用源URL来唯一定位资源,对资源的增删改查映射为HTTP的四个方法,无状态传输,具有非常好的伸缩性

     Apache Wink就是一个纯Java的REST框架。它完整的实现了JSR 311并扩展了部分功能,此外还提供了良好的扩展性,难能可贵的是还可以与流行的Java框架Spring无缝集成。目前该项目还在开发中。所谓框架无非就是定义好格式,提供一些工具和钩子,让开发人员可以专注于业务逻辑的开发。本文将试图简单的介绍一下Wink。

     Web Service当然是Web程序了,所以入口就是一个Servlet,在web.xml里面配置一下,把REST的访问都安排给Wink来处理。代码如下:

      <servlet><servlet-name>restSdkService</servlet-name><servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class><load-on-startup>0</load-on-startup></servlet><servlet-mapping><servlet-name>restSdkService</servlet-name><url-pattern>/rest/*</url-pattern></servlet-mapping>
     

    与Spring的集成,需要一个集成模块wink-spring-support,配置如下:

   <context-param><param-name>contextConfigLocation</param-name><param-value>classpath:META-INF/server/wink-core-context.xml,/WEB-INF/spring/app-context.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

其中wink-core-context.xml是wink-spring-support模块里面的一个Spring配置文件,app-context.xml配置如下:

   <bean class="org.apache.wink.spring.Registrar"><property name="classes"><set value-type="java.lang.Class"></set></property><property name="instances"><set><ref local="helloWorldResource" /></set></property></bean><!-- Resources --><bean id="helloWorldResource" class="HelloWorldResource" />

HelloWorldResource是一个REST里面的Resource,用Annotation配置路径等信息:

import javax.ws.rs.GET;import javax.ws.rs.Path;import org.apache.wink.common.annotations.Workspace;@Workspace(workspaceTitle = "Workspace Title", collectionTitle = "Collection Title")@Path("/helloworld")public class HelloWorldResource {@GETpublic String getMessage() {return "HelloWorld";}}
这样启动web服务器,输入http://localhost:8080/weat/rest/即可看到web service信息。服务列表里面可以看到HelloWorldResource服务。

原创粉丝点击