บทนำ (Overview)
บทความนี้เราจะนำเสนอการสร้าง “Path” สำหรับ “Restful Web Service” ส่งผลให้ใน 1 “Class” สามารถเรียกใช้ฟังก์ชันได้หลากหลายและมากกว่า 1 ฟังก์ชัน
ขั้นตอน (Steps)
- “Source-code” จากบทความที่แล้ว (How to create RESTful Web service with Netbeans – Create new services) จะเห็นได้ว่า “Service” ถูกกำหนดชื่อ “Path” ว่า “Login”
package helloworld; import javax.ws.rs.GET; import javax.ws.rs.Path; @Path("Login") public class LoginResource { @GET public String checkLogin() { return "Success"; } }
- เพราะฉะนั้นเวลาเรียกใช้ “Service” จะมี “URL” คือ (http://localhost:8080/Server/resources/Login) และส่งค่ากลับคืนคือ “Success”
- ทีนี้ลองเพิ่ม “Function” ใหม่ขึ้นมา และเพิ่ม “Path” ใหม่ขึ้นมาชื่อว่า “Forgot”
package helloworld; import javax.ws.rs.GET; import javax.ws.rs.Path; @Path("Login") public class LoginResource { @GET public String checkLogin() { return "Success"; } @GET @Path("/Forgot") public String forgotPassword() { return "E-mail sent already"; } }
- ทดลองเรียก URL (http://localhost:8080/Server/resources/Login/Forgot) จะผลลัพท์ดังรูป