Spring MVC Restful Service
Hi friends .Today i am going to demonstrate you how spring negotiates with various kinds of content such as xml,json.however there are two ways through which spring handles content.one is using @ResponseBody and another is using predefined view resolvers. i am going to demonstrate you how internal conversion is hanlded by @ResponseBody.i am showing example of how @ResponseBody converts incoming xml data into xml form.
Here is sample code:
Student.java
package org.demo.model;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="student")
public class Student
{
private int id;
private String name;
private List<College> collegeList;
public List<College> getCollegeList()
{
return collegeList;
}
@XmlElementWrapper(name="CollegeList")
public void setCollegeList(List<College> collegeList)
{
this.collegeList = collegeList;
}
public Student() {}
public Student(int id,String name)
{ this.id=id; this.name=name; }
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;
}
}
College.java
package org.demo.model;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="org.demo.model.Student")
public class College
{
private int id;
private String collegename;
public College() {}
public College(int id,String collegename) {super();this.id=id; this.collegename=collegename;}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCollegename() {
return collegename;
}
public void setCollegename(String collegename) {
this.collegename = collegename;
}
}
XmlController.java
package org.demo.controller;
import java.util.ArrayList;
import java.util.List;
import org.demo.model.College;
import org.demo.model.Employee;
import org.demo.model.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/Student")
public class XmlController
{
//invoke in such a manner : http://localhost:8080/SpringXmlJson/Student/1/nisarg
@RequestMapping(value="{id}/{name}", method = RequestMethod.GET,produces="application/xml")
public @ResponseBody Student getStudentInXML(@PathVariable int id,@PathVariable String name)
{
Student student1 = new Student(id, name);
College college1= new College(1,"semcom");
College college2=new College(2,"NVPAS");
List<College> l1 =new ArrayList<>();
l1.add(college1);
l1.add(college2);
student1.setCollegeList(l1);
return student1;
}
/* @RequestMapping(value="/Json/{id}/{name}/{age}", method = RequestMethod.GET,produces="application/json")
public @ResponseBody Employee getStudentInJson(@PathVariable int id,@PathVariable String name,@PathVariable String age)
{
Employee employee1= new Employee(id,name,age);
return employee1;
}*/
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringXmlJson</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>xmltest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xmltest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Context File(xmltest-servlet.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="org.demo.controller" />
<mvc:annotation-driven/>
</beans>
the output will look like as given below:
however i found one bug here.i can't able to get json data if jackson & jaxb jars are already there in classpath.it still gives me same output in xml format.
Hope you will get my point for any query ping me on pathak.nisarg@yahoo.com
thanks for reading this article.
regards
Hi friends .Today i am going to demonstrate you how spring negotiates with various kinds of content such as xml,json.however there are two ways through which spring handles content.one is using @ResponseBody and another is using predefined view resolvers. i am going to demonstrate you how internal conversion is hanlded by @ResponseBody.i am showing example of how @ResponseBody converts incoming xml data into xml form.
Here is sample code:
Student.java
package org.demo.model;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="student")
public class Student
{
private int id;
private String name;
private List<College> collegeList;
public List<College> getCollegeList()
{
return collegeList;
}
@XmlElementWrapper(name="CollegeList")
public void setCollegeList(List<College> collegeList)
{
this.collegeList = collegeList;
}
public Student() {}
public Student(int id,String name)
{ this.id=id; this.name=name; }
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;
}
}
College.java
package org.demo.model;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="org.demo.model.Student")
public class College
{
private int id;
private String collegename;
public College() {}
public College(int id,String collegename) {super();this.id=id; this.collegename=collegename;}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCollegename() {
return collegename;
}
public void setCollegename(String collegename) {
this.collegename = collegename;
}
}
XmlController.java
package org.demo.controller;
import java.util.ArrayList;
import java.util.List;
import org.demo.model.College;
import org.demo.model.Employee;
import org.demo.model.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/Student")
public class XmlController
{
//invoke in such a manner : http://localhost:8080/SpringXmlJson/Student/1/nisarg
@RequestMapping(value="{id}/{name}", method = RequestMethod.GET,produces="application/xml")
public @ResponseBody Student getStudentInXML(@PathVariable int id,@PathVariable String name)
{
Student student1 = new Student(id, name);
College college1= new College(1,"semcom");
College college2=new College(2,"NVPAS");
List<College> l1 =new ArrayList<>();
l1.add(college1);
l1.add(college2);
student1.setCollegeList(l1);
return student1;
}
/* @RequestMapping(value="/Json/{id}/{name}/{age}", method = RequestMethod.GET,produces="application/json")
public @ResponseBody Employee getStudentInJson(@PathVariable int id,@PathVariable String name,@PathVariable String age)
{
Employee employee1= new Employee(id,name,age);
return employee1;
}*/
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringXmlJson</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>xmltest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xmltest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Context File(xmltest-servlet.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="org.demo.controller" />
<mvc:annotation-driven/>
</beans>
the output will look like as given below:
however i found one bug here.i can't able to get json data if jackson & jaxb jars are already there in classpath.it still gives me same output in xml format.
Hope you will get my point for any query ping me on pathak.nisarg@yahoo.com
thanks for reading this article.
regards