翻譯|使用教程|編輯:李爽夏|2018-12-19 09:25:56.000|閱讀 303 次
概述:本教程示例將創(chuàng)建一個(gè)簡(jiǎn)單的Web服務(wù)來(lái)維護(hù)客戶列表。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
MyEclipse 在線訂購(gòu)年終抄底促銷!
使用開發(fā)RESTWeb服務(wù)來(lái)放大您的Web應(yīng)用程序。在本教程示例中,您將創(chuàng)建一個(gè)簡(jiǎn)單的Web服務(wù)來(lái)維護(hù)客戶列表。你將學(xué)會(huì):
沒有MyEclipse?
現(xiàn)在,您需要為您使用向?qū)?創(chuàng)建的方法提供實(shí)現(xiàn)。在真實(shí)的應(yīng)用程序中,此時(shí)您可能使用JPA或Hibernate連接數(shù)據(jù)庫(kù),以幫助管理客戶列表,但是對(duì)于本教程來(lái)說(shuō),簡(jiǎn)單的內(nèi)存映射就足夠了。
實(shí)現(xiàn)很簡(jiǎn)單;當(dāng)服務(wù)接收到客戶時(shí),您給實(shí)體一個(gè)基于計(jì)數(shù)器的id,并將其添加到映射。通過(guò)id從這個(gè)映射中檢索客戶并提供客戶列表非常簡(jiǎn)單,如下所示。
使用以下代碼替換CustomersResource類中的內(nèi)容。注意類和方法簽名沒有改變。您正在用服務(wù)的實(shí)現(xiàn)充實(shí)生成的存根。為了演示的目的,您還向列表中添加了一個(gè)客戶。
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 TreeMap<Integer, Customer> customerMap = new TreeMap<Integer, Customer>(); 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<Customer> getCustomers() { List<Customer> customers = new ArrayList<Customer>(); 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 Browser打開Web服務(wù)應(yīng)用程序的默認(rèn)index.jsp頁(yè)面。您不需要它,因?yàn)?您沒有測(cè)試網(wǎng)頁(yè),所以可以關(guān)閉此視圖。
REST Web服務(wù)資源管理器在MyEclipse標(biāo)準(zhǔn)訂閱級(jí)別不可用。如果您是MyEclipse標(biāo)準(zhǔn)訂戶,請(qǐng)按照使用標(biāo)準(zhǔn)瀏覽器測(cè)試Web服務(wù)的說(shuō)明操作。
<customer> <name>Bill Adama</name> <address>Vancouver, Canada</address> </customer>
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都控件網(wǎng)