首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

创办一个最简单的restlet webservice

2012-07-20 
创建一个最简单的restlet webserviceOperationServer1方法里:public class OperationServer1 extends Serv

创建一个最简单的restlet webservice
OperationServer1方法里:

public class OperationServer1 extends ServerResource {static String s = "operation1";@Get()public Representation doGet(Representation entity){s = "Get Operation1";StringRepresentation entityRsp=new StringRepresentation(s, MediaType.TEXT_XML);return entityRsp;}@Post()public Representation doPost(Representation entity) {s = "Post Operation1";StringRepresentation entityRsp=new StringRepresentation(s, MediaType.TEXT_XML);return entityRsp;    }@Put()public Representation doPut(Representation entity){s = "Put Operation1";StringRepresentation entityRsp=new StringRepresentation(s, MediaType.TEXT_XML);return entityRsp;}@Delete()public Representation doDelete(Representation entity){s = "Delete Operation1";StringRepresentation entityRsp=new StringRepresentation(s, MediaType.TEXT_XML);return entityRsp;}}

其中extends ServerResource不能缺少,否则会报错cannot be cast to org.restlet.resource.ServerResource

WEB-INF下web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app><display-name>restClientExample</display-name><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/xml/restApi.xml          </param-value></context-param><context-param><param-name>logbackConfigLocation</param-name><param-value>/WEB-INF/xml/logback.xml</param-value></context-param><filter>      <filter-name>Spring character encoding filter</filter-name>      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>      <init-param>          <param-name>encoding</param-name>          <param-value>utf-8</param-value>  </init-param><init-param>         <param-name>forceEncoding</param-name>         <param-value>true</param-value>               </init-param>    </filter>  <filter-mapping>      <filter-name>Spring character encoding filter</filter-name>      <url-pattern>/*</url-pattern>  </filter-mapping> <listener><listener-class>ch.qos.logback.classic.servlet.LogbackConfigListener</listener-class></listener><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>restlet</servlet-name><servlet-class>org.restlet.ext.spring.SpringServerServlet</servlet-class><init-param><param-name>org.restlet.component</param-name><param-value>security_component</param-value></init-param></servlet><servlet-mapping><servlet-name>restlet</servlet-name><url-pattern>/*</url-pattern></servlet-mapping></web-app>

其中webapp标签下的各个标签顺序要按照要求来否则会报错The content of element type "web-app" must match "(icon?,display-name?,description?,distributable?,context-param*

xml目录下restApi.xml:
<?xml version="1.0" encoding="UTF-8"?>    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">    <beans>    <bean id="security_component" ref="security_restRoute" />      </bean>         <bean id="security_restRoute" bean="Operation1" />                   </bean>                  </entry>                <entry key="/operation2">                    <bean bean="Operation2" />                   </bean>                  </entry>            </map>          </property>      </bean> <bean id="Operation1" scope="prototype"></bean><bean id="Operation2" scope="prototype"></bean></beans>


当然,有多个接口是,新增新的java类,在restApi.xml也相应添加新的bean即可。

其实如果有些公共类(所有URI都要使用到的),应该新写个server类,让其继承ServerResource,在这个sever类里初始化这些公有类,然后其他的URI都继承这个server类。
则restApi.xml需要修改下:
<bean id="fatherSever" ref="公有类"/>    </bean><bean id="Operation1" scope="prototype" parent="fatherSever"></bean>

热点排行