How to create RESTful Web service with Netbeans – XML

บทนำ (Overview)

จากบทความที่แล้วเราสามารถสร้าง “Class” ขึ้นสำหรับเก็บข้อมูล “Employee” และส่งค่าดังกล่าวกลับไปยัง “Client” (How to create RESTful Web service with Netbeans – Create New Class) แต่ทว่า ก่อนจะส่งไปนั้นจะต้องดึงค่าเป็น String แล้วนำมาต่อ ๆ กันก่อนส่งไป ในบทความนี้เราจะประยุกต์ใช้เอกสาร “XML” ซึ่งมีลัษณะโครงสร้างข้อมูลที่เหมาะสมกับ Class ที่เราสร้างขึ้นมา แทนที่ Server จะต้องดึงค่าที่ละฟังก์ชัน ก็ส่งกลับไปหา Client ในลักษณะ Class

ขั้นตอน (Steps)

  1. ก่อนอื่นเริ่มต้นต้องเพิ่มคำสั่งดังนี้
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    
  2. จากนั้นเราสามารถเรียกใช้งาน “@Produces” สำหรับกำหนดประเภทของสื่อที่ใช้ประเภท “XML” สำหรับ “Function” ดังนี้
    @Produces(MediaType.APPLICATION_XML)
    
  3. จาก “Source-code” ในบทความที่แล้วสามารถปรับเปลี่ยนเล็กน้อยดังนี้ (How to create RESTful Web service with Netbeans – Create New Class)
        @GET
        @Path("/xml/{id}")
        @Produces(MediaType.APPLICATION_XML) //กำหนดประเภทสื่อ
    
        //เปลี่ยนค่า Return ของเดิม จาก String เป็น Class ที่ชื่อ Employee
        public Employee getEmpDetail(@PathParam("id") String id) {
            Employee emp = new Employee();
            emp.setId(id);
            emp.setName("Warunyou");
            emp.setDepartment("IT security");
            
            return emp; //return ทั้ง Class
        }
    
  4. และในส่วนของไฟล์ ไฟล์ “Employee.java” ปรับเปลี่ยนให้สามารถใช้งานคุณสมบัติไฟล์ “XML” โดยต้องเพิ่ม “code” ดังนี้
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlElement;
    
  5. เพื่อ “Tag” กำหนด “Root” ของเอกสาร “XML”
    @XmlRootElement(name = "Employee")
    public class Employee {
    
  6. กำหนด “Attribute” ให้กับ “Root”
    @XmlAttribute
    public String getId() {
    
  7. สุดท้ายลองกำหนด “Entity” หรือ “Properties” ของ “Class” ทั้งหมด
        @XmlElement
        public String getName() {
            return name;
        }
     
        public void setName(String name) {
            this.name = name;
        }
        
        @XmlElement
        public String getDepartment(){
            return department;
        }
        
        public void setDepartment(String depart) {
            this.department = depart;
        }
    
  8. สามารถสรุป “Source-code” ทั้งหมดได้ดังนี้
    LoginResource.java

    package helloworld;
    
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    
    @Path("Login")
    public class LoginResource {
        @GET
        @Path("/xml/{id}")
        @Produces(MediaType.APPLICATION_XML)
        public Employee getEmpDetail(@PathParam("id") String id) {
            Employee emp = new Employee();
            emp.setId(id);
            emp.setName("Warunyou");
            emp.setDepartment("IT security");
            return emp;
        }
    }
    
    

    Employee.java

    package helloworld;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlElement;
    
    @XmlRootElement(name = "Employee")
    public class Employee {
        
        String id;
        String name;
        String department;
        
        @XmlAttribute
        public String getId() {
            return id;
        }
     
        public void setId(String id) {
            this.id = id;
        }
        
        @XmlElement
        public String getName() {
            return name;
        }
     
        public void setName(String name) {
            this.name = name;
        }
        
        @XmlElement
        public String getDepartment(){
            return department;
        }
        
        public void setDepartment(String depart) {
            this.department = depart;
        }
        
    }
    
  9. ลองเข้าถึง URL (http://localhost:8080/Server/resources/Login/xml/2543) ได้ผลลัพธ์ดังนี้ restservice-netbean43

ใส่ความเห็น