RESTLET开发实例(三)基于spring的REST服务
http://www.lifeba.org/arch/restlet_spring_3.html
前面兩篇文章,我們介紹了基于JAX-RS的REST服務以及Application的Rest服務。這里將介紹restlet如何整合spring框架進行開發。Spring 是一個開源框架,是為了解決企業應用程序開發復雜性而創建的,廣泛的應用于應用項目中。本篇文章將在上篇的基礎上面做介紹,將上篇文章的介紹的rest,用spring整合來實現。
一、基于spring配置的Rest簡單服務
1、新建RestSpringApplication Web工程。
將restlet和spring的jar包復制進來。紅色部分為新加入進來的jar包。
將上篇中的RestApplication工程項目中的src的源文件復制過來。
2、將web.xml加入下面代碼
<servlet>
<servlet-name>restlet</servlet-name>
<servlet-class>
org.restlet.ext.spring.RestletFrameworkServlet
</servlet-class>
<init-param>
<param-name>org.restlet.component</param-name>
<param-value>restletComponent</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>restlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
上面代碼指定了restlet使用spring的RestletFrameworkServlet。
3、建立restlet-servlet.xml文件,只需要配置org.restlet.ext.spring.SpringRouter,及對應的路徑和資源文件。
<bean name="root">
<property name="attachments">
<map>
<entry key="/student/{studentId}">
<bean>
<lookup-method name="create" bean="StudentResource" />
</bean>
</entry>
<entry key="/student">
<bean>
<lookup-method name="create" bean="StudentsResource" />
</bean>
</entry>
</map>
</property>
</bean>
<bean id="StudentResource" class="org.lifeba.ws.resource.StudentResource" scope="prototype"/>
<bean id="StudentsResource" class="org.lifeba.ws.resource.StudentsResource" scope="prototype"/>
上面的代碼配置了 /student/{studentId}對應StudentResource,以及student對應StudentsResource資源類。通過 SpringRouter可以非常方便的通過attachments配置資源路徑。如果你有更多的路徑,你可以建立多個entry即可。
4、好了,你現在可以重啟tomcat了,輸入 http://localhost:8085/RestSpringApplication/student/1 ,訪問正常。
二、測試添加、刪除、更新方法。
1、student_post方法,添加一個Student,成功后返回新建studentId為2的對象。
public void student_post(){
try {
Form queryForm = new Form();
queryForm.add("name","steven_spring");
queryForm.add("clsId","201002");
queryForm.add("sex","2");
queryForm.add("age","12");
ClientResource client = new ClientResource("http://localhost:8085/RestSpringApplication/student");
Representation representation =client.post(queryForm.getWebRepresentation());
System.out.println(representation.getText());
} catch (Exception e) {
e.printStackTrace();
}
}
訪問http://localhost:8085/RestSpringApplication/student/2
訪問http://localhost:8085/RestSpringApplication/student ,可以看到有2個Student對象。
2、student_delete方法,刪除Id為1的Student,成功執行后返回1。
public void student_delete(){
try {
ClientResource client = new ClientResource("http://localhost:8085/RestSpringApplication/student/1");
Representation representation =client.delete();
System.out.println(representation.getText());
} catch (Exception e) {
e.printStackTrace();
}
}
再次訪問http://localhost:8085/RestSpringApplication/student,可以看到只有一個Id為2的Student對象。
3、student_put方法,更新Id為2的Student。
public void student_put(){
try {
Form queryForm = new Form();
queryForm.add("name","steven_spring_modify");
queryForm.add("clsId","201012");
queryForm.add("sex","12");
queryForm.add("age","24");
ClientResource client = new ClientResource("http://localhost:8085/RestSpringApplication/student/2");
Representation representation =client.put(queryForm);
System.out.println(representation.getText());
} catch (Exception e) {
e.printStackTrace();
}
}
訪問http://localhost:8085/RestSpringApplication/student/2
通過上面的代碼已經完全實現了Spring中的restlet的配置。上面只對Student對象做了介紹,你也可以實現對Course在spring中配置,基本方法一樣。這里不再闡述。
三、資源工程下載
RestSpringApplication工程
總結
以上是生活随笔為你收集整理的RESTLET开发实例(三)基于spring的REST服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 王者荣耀赵云怎么玩 赵云实战玩法攻略分享
- 下一篇: 创造与魔法氤氲之息刷新位置在哪里?创造与