Featured Post

restful web service json response


How can we use Spring to create Restful Web Service returning JSON response?

For adding JSON support to your spring application, you will need to add Jackson dependency in first step.


    com.fasterxml.jackson.core
    jackson-databind
    2.4.1

Now you are ready to return JSON response from your MVC controller. All you have to do is return JAXB annotated object from method and use @ResponseBody annotation on this return type.
@Controller
public class EmployeeRESTController
{
 @RequestMapping(value = "/employees")
 public @ResponseBody EmployeeListVO getAllEmployees()
 {
  EmployeeListVO employees = new EmployeeListVO();
  //Add employees
  return employees;
 }
}
Alternatively, you can use @RestController annotation in place of @Controller annotation. This will remove the need to using @ResponseBody.
@RestController = @Controller + @ResponseBody
So you can write the above controller as below.
@RestController
public class EmployeeRESTController
{
 @RequestMapping(value = "/employees")
 public EmployeeListVO getAllEmployees()
 {
  EmployeeListVO employees = new EmployeeListVO();
  //Add employees
  return employees;
 }
}

Comments

Popular posts from this blog

[Inside AdSense] Understanding your eCPM (effective cost per thousand impress...