文章翻譯自Genuitec學(xué)習(xí)中心
在本例中,將創(chuàng)建一個(gè)管理顧客的簡單Web Service
1. 創(chuàng)建一個(gè)REST Web Service項(xiàng)目
從項(xiàng)目的配置開始REST Web Service:
1. 打開MyEclipse后,依次點(diǎn)擊File->New->Web Service Project;或者點(diǎn)擊工具條上的新建圖標(biāo)
2. 在Project name框中輸入restdemo觉痛,WebServices Framework選擇REST(JAX-RS)框架,然后點(diǎn)擊next進(jìn)入下一步立宜;
- 接受默認(rèn)的文件夾路徑千绪,一路點(diǎn)擊下一步娃兽;
- 在配置web module部分可以根據(jù)需要將Generate web.xml deployment descriptor勾選上朽肥,建議勾上禁筏,點(diǎn)擊finish完成項(xiàng)目創(chuàng)建
2. 創(chuàng)建一個(gè)新的REST Web Service
在創(chuàng)建好的項(xiàng)目中,開始創(chuàng)建Web服務(wù)衡招,我們將為管理顧客創(chuàng)建實(shí)體類和操作資源的方法類篱昔。
2.1 創(chuàng)建顧客的實(shí)體類
這個(gè)簡單的顧客類包含id,name和address三個(gè)字段蚁吝,用來代表Web Service服務(wù)的顧客對象旱爆。
1. 在之前創(chuàng)建好的restdemo項(xiàng)目上右鍵點(diǎn)擊舀射,選擇New->Class
2. 在Package中輸入com.myeclipseide.ws窘茁,在Name中輸入Customer,如果public static void main復(fù)選框被選中脆烟,取消它山林,然后點(diǎn)擊finish
3. 用下面的代碼替換默認(rèn)生成的內(nèi)容,然后保存
package com.myeclipseide.ws;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Customer {
private int id;
private String name;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
本例中使用XML作為序列化格式,即使用XML從Web服務(wù)發(fā)送和接收客戶實(shí)體驼抹。
Customer類中的@XMLRootElement注釋是一個(gè)JAXB注釋桑孩,允許JAXB將該實(shí)體從Java轉(zhuǎn)換為XML并返回。也可以注釋類中的字段和方法來自定義序列化框冀,但是在本例中流椒,默認(rèn)的JAXB就夠了。
2.2創(chuàng)建資源類明也,Web Service的關(guān)鍵
- 右鍵點(diǎn)擊restdemo項(xiàng)目宣虾,選擇New->Other;
- 在篩選框中輸入web service,選中Web Service,然后點(diǎn)擊next温数;
3. 在彈出的向?qū)υ捒蛑行逑酰琒trategy選擇Create web service from Java class,同時(shí)選中Create new Java bean復(fù)選框
4. 在URL path框中輸入customers,Lifecycle選項(xiàng)選擇Singleton撑刺,從Produces的下拉選項(xiàng)中選擇application/xml鹉胖,點(diǎn)擊Java package右側(cè)的Browse選擇com.myeclipseide.ws(或者輸入),Java Class輸入CustomersResource
URL路徑 - 指示可以到達(dá)該資源的路徑够傍。在本例中甫菠,我們使用customers,因?yàn)樵撡Y源管理我們的客戶列表冕屯,顧客資源將被托管在“/ customers”淑蔚。
Singleton Lifecycle - 確保在單個(gè)Web應(yīng)用程序中此類只會(huì)被Jersey創(chuàng)建一個(gè)實(shí)例。
Consumes and Produces - 用于指定此資源可以接受和生成的默認(rèn)MIME類型的數(shù)據(jù)愕撰。這些值也可以被類中的各個(gè)方法覆蓋刹衫。如上所述,我們將數(shù)據(jù)序列化為XML搞挣,所以使用application / xml的MIME類型带迟。
- 點(diǎn)擊Add按鈕,為Java類添加獲取所有顧客的方法囱桨;在Method name中輸入方法名getCustomers仓犬,返回類型輸入java.util.List<Customer>,然后點(diǎn)擊finish
6. 再次點(diǎn)擊Add按鈕舍肠,為Java類添加一個(gè)獲取特定顧客信息的方法搀继;方法名輸入getCustomer,返回類型輸入Customer翠语,URL path輸入{id}叽躯;
- 然后點(diǎn)擊該對話框的Add按鈕,為方法添加參數(shù)肌括,參數(shù)值直接在表格中輸入和選擇点骑,如下圖所示,然后點(diǎn)擊finish
- 再添加一個(gè)增加顧客的方法,操作如上黑滴,具體參數(shù)如下圖所示憨募;
9. 在加入三個(gè)方法之后,配置頁面應(yīng)該如下圖所示
10. 點(diǎn)擊finish生成CustomerResource類袁辈,打開文件可以查看生成的方法菜谣。
3. 具體實(shí)現(xiàn)生成的方法
現(xiàn)在,我們需要為這些方法實(shí)現(xiàn)具體的業(yè)務(wù)邏輯晚缩,本例中沒有使用數(shù)據(jù)庫來管理顧客列表葛菇,為了方便只是使用簡單的map來存儲(chǔ)。用下面的代碼替換CustomersResource類文件中的內(nèi)容
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 javax.ws.rs.core.MediaType;
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(){
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(MediaType.TEXT_PLAIN)
@Consumes(MediaType.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;
}
}
4. 發(fā)布 Web Service應(yīng)用
發(fā)布web service最快的方法是使用Run As或者Debug As MyEclipse Server Application.
1. 右鍵點(diǎn)擊項(xiàng)目橡羞,選擇Debug As(或者Run As)->MyEclipse Server Application.
2. 選擇Myeclipse Tomcat(或自己加的Tomcat)眯停,然后點(diǎn)擊finish,完成發(fā)布
5. 瀏覽器中測試REST Web Service
到這步咱們的web service基本完成了卿泽,現(xiàn)在在瀏覽器中測試下莺债。
1. 打開瀏覽器,在地址欄中輸入http://localhost:<port>/<project name>/jaxrs/<your url path>/0
返回如下圖的信息签夭,表明成功齐邦。
2. 再測試下Add操作是否正常,使用chrome瀏覽器的Postman插件第租。
- 輸入
http://localhost:8080/restdemo/jaxrs/customers/add
- 選擇POST方法
- 在Headers中Key選擇Content-Type,Value選擇application/xml
- 在Body中輸入需要增加的顧客信息xml形式
- 點(diǎn)擊Send按鈕措拇,完成添加
3.在瀏覽器中輸入http://localhost:8080/restdemo/jaxrs/customers/,得到下圖結(jié)果,看到添加成功