當前位置:編程學習大全網 - 源碼下載 - java容器中有哪些restlet?

java容器中有哪些restlet?

首先,基於spring配置的簡單Rest服務

1,新建RestSpringApplication Web項目。

復制restlet和spring的jar包。紅色部分是新添加的jar包。

復制上壹篇文章中RestApplication項目中src的源文件。

2.將web.xml添加到以下代碼中。

& ltservlet & gt

& ltservlet-name & gt;restlet & lt/servlet-name & gt;

& ltservlet-class & gt;

org . rest let . ext . spring . restletframeworkservlet

& lt/servlet-class & gt;

& ltinit-param >

& ltparam-name & gt;org . rest let . component & lt;/param-name & gt;

& lt參數值& gtrestletComponent & lt/param-value & gt;

& lt/init-param & gt;

& lt/servlet & gt;

& ltservlet映射& gt

& ltservlet-name & gt;restlet & lt/servlet-name & gt;

& lturl模式& gt/* & lt;/URL-pattern & gt;

& lt/servlet-mapping & gt;

上面的代碼指定restlet使用spring的RestletFrameworkServlet。

3.要建立restlet-servlet.xml文件,只需要配置org . restlet . ext . spring . spring router以及對應的路徑和資源文件。

& ltbean name = " root " & gt

& ltproperty name="attachments " >

& lt地圖& gt

& ltentry key = "/student/{ student id } " & gt;

& ltbean & gt

& ltlookup-method name = " create " bean = " StudentResource "/& gt;

& lt/bean & gt;

& lt/entry & gt;

& ltentry key = "/student " & gt;

& ltbean & gt

& ltlookup-method name = " create " bean = " students resource "/>

& lt/bean & gt;

& lt/entry & gt;

& lt/map & gt;

& lt/property & gt;

& lt/bean & gt;

& ltbean id = " StudentResource " class = " org . life ba . ws . resource . StudentResource " scope = " prototype "/& gt;

& ltbean id = " students resource " class = " org . life ba . ws . resource . students resource " scope = " prototype "/& gt;

上面的代碼配置了StudentResource對應的/student/{studentId},studentresource資源類對應的student。通過SpringRouter通過附件配置資源路徑非常方便。如果有更多路徑,可以創建多個條目。

4.好了,現在可以重啟tomcat了。輸入http://localhost:8085/restspring application/student/1,訪問正常。

第二,測試添加、刪除和更新的方法。

1,Student_post方法,添加壹個學生,成功後返回新創建的studentId為2的對象。

public void student_post(){

嘗試{

Form query Form = new Form();

queryForm.add("name "," Steven _ spring ");

queryForm.add("clsId "," 201002 ");

queryForm.add("sex "," 2 ");

queryForm.add("age "," 12 ");

client resource client = new client resource(" http://localhost:8085/restspring application/student ");

re presentation re presentation = client . post(query form . get web re presentation());

system . out . println(re presentation . gettext());

} catch(異常e) {

e . printstacktrace();

}

}

請訪問http://localhost:8085/restspring application/student/2。

訪問http://localhost:8085/restspringapplication/Student,可以看到有兩個學生對象。

2.Student_delete方法刪除Id為1的學生,執行成功後返回1。

public void student_delete(){

嘗試{

client resource client = new client resource(" http://localhost:8085/restspring application/student/1 ");

表象表象= client . delete();

system . out . println(re presentation . gettext());

} catch(異常e) {

e . printstacktrace();

}

}

再次訪問http://localhost:8085/restspringapplication/Student,可以看到只有壹個Id為2的Student對象。

3.Student_put方法更新Id為2的學生。

public void student_put(){

嘗試{

Form query Form = new Form();

queryForm.add("name "," Steven _ spring _ modify ");

queryForm.add("clsId "," 201012 ");

queryForm.add("sex "," 12 ");

queryForm.add("age "," 24 ");

client resource client = new client resource(" http://localhost:8085/restspring application/student/2 ");

表象表象= client . put(query form);

system . out . println(re presentation . gettext());

} catch(異常e) {

e . printstacktrace();

}

}

請訪問http://localhost:8085/restspring application/student/2。

通過上面的代碼,已經完全實現了Spring中restlet的配置。以上只是介紹了學生對象,妳也可以用同樣的方法在spring中配置課程。這裏就不細說了。

  • 上一篇:車禍網源代碼
  • 下一篇:有哪些打開信封破解和知道每壹輪數字的技巧?
  • copyright 2024編程學習大全網