日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

RESTLET开发实例(三)基于spring的REST服务

發布時間:2023/12/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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服务的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。