翻譯|使用教程|編輯:董玉霞|2022-06-29 15:49:33.320|閱讀 128 次
概述:本次MyEclipse使用教程介紹了部署 Web 服務(wù)應(yīng)用程序的相關(guān)內(nèi)容。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
本教程需要下載MyEclipse最新版本 。
在MyEclipse中開發(fā) REST Web 服務(wù)以增強您的 Web 應(yīng)用程序。在本教程示例中,您將創(chuàng)建一個簡單的 Web 服務(wù)來維護客戶列表。您將學(xué)會:
現(xiàn)在,您需要為使用向?qū)?chuàng)建的方法提供實現(xiàn)。在實際應(yīng)用程序中,此時您可能會使用 JPA 或 Hibernate 連接到數(shù)據(jù)庫以幫助管理客戶列表,但是對于本教程來說,一個簡單的內(nèi)存映射就足夠了。
實現(xiàn)很簡單;當(dāng)服務(wù)接收到客戶時,您為實體提供基于計數(shù)器的 id 并將其添加到地圖中。通過 id 從該地圖中檢索客戶并提供客戶列表非常簡單,如下所示。
將 CustomersResource 類中的內(nèi)容替換為以下代碼。觀察類和方法簽名沒有改變。您正在使用服務(wù)的實現(xiàn)來充實生成的存根。出于演示目的,您還可以將單個客戶添加到列表中。
package com.myeclipseide.ws; import java.util.ArrayList; import java.util.List; import java.util.TreeMap; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import com.sun.jersey.spi.resource.Singleton; @Produces("application/xml") @Path("customers") @Singleton public class CustomersResource { private TreeMapcustomerMap = new TreeMap (); public CustomersResource() { // hardcode a single customer into the database for demonstration // purposes Customer customer = new Customer(); customer.setName("Harold Abernathy"); customer.setAddress("Sheffield, UK"); addCustomer(customer); } @GET public List getCustomers() { List customers = new ArrayList(); customers.addAll(customerMap.values()); return customers; } @GET @Path("{id}") public Customer getCustomer(@PathParam("id") int cId) { return customerMap.get(cId); } @POST @Path("add") @Produces("text/plain") @Consumes("application/xml") public String addCustomer(Customer customer) { int id = customerMap.size(); customer.setId(id); customerMap.put(id, customer); return "Customer " + customer.getName() + " added with Id " + id; } }
部署 Web 服務(wù)的最快方法是使用 Run As 或 Debug As MyEclipse Server Application 操作。
MyEclipse 執(zhí)行以下步驟:
MyEclipse Web 瀏覽器打開我們的 Web 服務(wù)應(yīng)用程序的默認index.jsp頁面。你不需要這個,因為你沒有測試一個網(wǎng)頁,所以你可以關(guān)閉這個視圖。
REST Web 服務(wù)資源管理器在 MyEclipse 標(biāo)準(zhǔn)訂閱級別不可用。如果您是 MyEclipse Standard 訂閱者,請按照使用標(biāo)準(zhǔn)瀏覽器測試 Web 服務(wù)中的說明進行操作。
注意:如果您將 restdemo 部署到 MyEclipse Tomcat 以外的應(yīng)用程序服務(wù)器,則資源管理器中使用的 WADL URL 可能包含不正確的端口,從而阻止資源管理器加載您的 WADL 文件。更正端口,然后單擊執(zhí)行按鈕繼續(xù)。
您還可以通過單擊工具欄上 Web 服務(wù)資源管理器圖標(biāo)上的下拉箭頭 并選擇啟動 REST Web 服務(wù)資源管理器來打開 REST Web 服務(wù)資源管理器。在這種情況下,您必須在地址欄中輸入 WADL 文件的路徑才能繼續(xù)操作。
<customer> <name>Bill Adama</name><address>Vancouver, Canada</address> </customer>
本次MyEclipse使用教程介紹了部署 Web 服務(wù)應(yīng)用程序的相關(guān)內(nèi)容,想要了解全部教程
MyEclipse V2022.1正式發(fā)布,大家可以下載MyEclipse官方正版試用,更多MyEclipse價格咨詢可聯(lián)系慧都在線客服。
MyEclipse是功能最全面、性價比最高的企業(yè)級Java IDE之一。
MyEclipseQQ群:742336981 歡迎進群一起討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn