服务的粒度越小复用度就越高。
本文提供一个restlet的最简单的实现。
一、首先实现Get方法和Post方法
1、Get方法
package org.soachina.rest.component.resource; import org.restlet.data.Status; import org.restlet.representation.Representation; import org.restlet.representation.StringRepresentation; import org.restlet.resource.Get; import org.restlet.resource.ServerResource; public class GetDemoResource extends ServerResource { @Get public Representation get() { getResponse().setStatus(Status.SUCCESS_OK); return new StringRepresentation("*** Get Method Done!"); } }
package org.soachina.rest.component.resource; import org.restlet.data.Status; import org.restlet.representation.Representation; import org.restlet.representation.StringRepresentation; import org.restlet.resource.Post; import org.restlet.resource.ServerResource; public class PostDemoResource extends ServerResource { @Post public Representation post(String parameters) { getResponse().setStatus(Status.SUCCESS_OK); return new StringRepresentation("*** Post Method Done!" + parameters); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>Restful Webservice Component</display-name> <description>Restful Webservice Component</description> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:config/application-context.xml </param-value> <description>Spring config file locations</description> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>RestletServlet</servlet-name> <servlet-class>org.restlet.ext.spring.SpringServerServlet</servlet-class> <init-param> <param-name>org.restlet.application</param-name> <param-value>application</param-value> </init-param> <init-param> <param-name>org.restlet.component</param-name> <param-value>component</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>RestletServlet</servlet-name> <url-pattern>/restlet/*</url-pattern> </servlet-mapping> <session-config> <session-timeout>30</session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
三、application-context.xml
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'> <beans> <import resource="classpath:config/rest/rest-component-config.xml"/> <import resource="classpath:config/extension/*-context.xml"/> </beans>
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'> <beans> <!-- ========================== Base Config ============================= --> <bean name="component" id="component" autowire="byName" class="org.restlet.ext.spring.SpringComponent"> <property name="defaultTarget" ref="application" /></bean> <bean name="application" id="application" class="org.restlet.Application"> <property name="inboundRoot" ref="router" /></bean> <bean name="router" class="org.restlet.ext.spring.SpringBeanRouter" /> <!-- ========================== URL Config ============================= --> <bean name="/getdemo" id="get" autowire="byName" class="org.soachina.rest.component.resource.GetDemoResource" /> <bean name="/postdemo" id="post" autowire="byName" class="org.soachina.rest.component.resource.PostDemoResource" /> </beans>
项目是一个zip压缩包,可以直接解压部署在tomcat6.x或者tomcat7.x上。
压缩包中有readme.txt,描述了如何安装、测试和修改。
源代码在上面第一步中提供。
下载地址: http://download.csdn.net/detail/teamlet/5258313