Saturday, 5 September 2015

XML response with @ResponseBody annotation

@ResponseBody annotation is front of controller and annotates the object that we want in xml form.

Return xml response of Java object controller

To return an java object in xml form from an spring objects requires two simple configurations:
1) Annotate Object Class
2) Add @ResponseBody annotation to the controller's method


1) Annotate Object Class

This is a simple Java class (Student) whose object will be returned in xml form, from the controller.

@XmlRootElement defines the root element of xml structure; by default it takes class name as root element.

Optionally we can give its "name" parameter if we want some other name as xml root element.

@XmlElement defines internal xml nodes, 'name' attribute applies here as well, as same as with @XmlRootElement.

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;

@XmlRootElement(name = "student")
public class Student {
       private int id;
       private String firstName;
       private String lastName;
       private String email;
       private String phone;

       protected Student() {
       }

       public Student(int id, String firstName, String lastName, String email,
                     String phone) {
              this.id = id;
              this.firstName = firstName;
              this.lastName = lastName;
              this.email = email;
              this.phone = phone;
       }

       public int getId() {
              return id;
       }

       @XmlElement
       public void setId(int id) {
              this.id = id;
       }

       public String getFirstName() {
              return firstName;
       }

       @XmlElement
       public void setFirstName(String firstName) {
              this.firstName = firstName;
       }

       public String getLastName() {
              return lastName;
       }

       @XmlElement
       public void setLastName(String lastName) {
              this.lastName = lastName;
       }

       public String getEmail() {
              return email;
       }

       @XmlElement
       public void setEmail(String email) {
              this.email = email;
       }

       public String getPhone() {
              return phone;
       }

       @XmlElement
       public void setPhone(String phone) {
              this.phone = phone;
       }
}

2) Add @ResponseBody annotation to the controller's method

Second thing we need to do is to use '@ResponseBody' annotation against the controller's method.

This will make spring understand that method return value should be bound to the web response body.

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DataController {

       @RequestMapping("student")
       public @ResponseBody Student getStudent() {
              return new Student(1, "Rajesh", "Kumar", "rk@gmail.com","9528716691");
       }

       @RequestMapping("studentlist")
       public @ResponseBody StudentList getStudentList() {
              List<student> studentList = new ArrayList<student>();
              studentList.add(new Student(2,"awdh","dixit", "awdh@gmail.com","8287713093"));
              studentList.add(new Student(1,"Rajesh","Kumar", "rk@gmail.com","9528716691"));

              return new StudentList(studentList);
       }
}


Return Java object List as xml response from controller

Retrun the simple xml form of single java object is quite simple because there is full control to 'Student' class and hence we could annotate the class easily.

When we want to return a list of objects say 'ArrayList', but we don’t have ArrayList class's code to annotate it.
So it’s a bit tricky; we have to make another class having a list of objects and the object of that annotated class will represent the whole list and will be returned as a list of xml nodes.

import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "student-list")
public class StudentList {

       private List<student> studentList;

       protected StudentList() {
       }

       public StudentList(List<student> studentList) {
              this.studentList = studentList;
       }

       @javax.xml.bind.annotation.XmlElement(name = "student")
       public List<student> getStudentList() {
              return studentList;
       }
}



No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...