REST架构之Apache Wink

Apache Wink是一个使用简单,稳定的Java框架,用于创建RESTful web services应用程序。Wink包括了一个服务器端模块和一个客户端模块,用于帮助开发者快速高效的开发RESTful Web services应用。

首先,需要在web.xml里面配置一下,把REST的访问都安排给Wink来处理。代码如下:

  <servlet>
    <servlet-name>restSdkService</servlet-name>
    <servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class>
  </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
        classpath:conf/spring-bean.xml
classpath:conf/spring-dataSource.xml
</param-value>
</context-param>

其中wink-core-context.xml是wink-spring-support模块里面的一个Spring配置文件,spring-bean.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="userRest" />
                <ref local="noticeRest" />
                <ref local="searchRest" />
                <ref local="voteRest" />
                <ref local="resourceRest" />
                <ref local="sysManageRest" />
            </set>
        </property>
    </bean>

    <bean  class="rest.UserRest">
    </bean>

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("/user")  
public class UserRest{  
  
    @GET  
    public String login() {  
        return "ok";  
    }  
}

以前,是用的cfx方式发布的restful服务。