XML/JSON POST with RequestBody in Spring REST Controller

I am creating a RESTful website with Spring 3.0. I am using ContentNegotiatingViewResolver as well as HTTP Message Convertors (like MappingJacksonHttpMessageConverter for JSON, MarshallingHttpMessageConverter for XML, etc.). I am able to get the XML content successfully, if I use the .xml suffix in the last of url and same in case of JSON with .json suffix in URL.

Getting XML/JSON contents from controller doesn't produce any problem for me. But, how can I POST the XML/JSON with request body in same Controller method?

For e.g.

@RequestMapping(method=RequestMethod.POST, value="/addEmployee")
   public ModelAndView addEmployee(@RequestBody Employee e) {
        employeeDao.add(e);
        return new ModelAndView(XML_VIEW_NAME, "object", e);
}

Answers


You should consider not using a View for returning JSON (or XML), but use the @ResponseBody annotation. If the employee is what should be returned, Spring and the MappingJacksonHttpMessageConverter will automatic translate your Employee Object to JSON if you use a method definition and implementation like this (note, not tested):

   @RequestMapping(method=RequestMethod.POST, value="/addEmployee")
   @ResponseBody
   public Employee addEmployee(@RequestBody Employee e) {
     Employee created = employeeDao.add(e);
     return created;
   }

Need Your Help

Quadtree and division into equal subquadrants

c quadtree

If I want to traverse a quadtree, do the dimensions have to be only 2^n ?

Android: Specify String in different Package in Manifest

android

Is it possible to specify that a @string value belongs to a different package via the Manifest?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.