XML/JSON POST with RequestBody in Spring REST Controller

33,787

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;
   }
Share:
33,787
Arun Kumar
Author by

Arun Kumar

Experienced Team Leader / Senior Software Developer with a demonstrated history of working in domains like Banking & Financial services, Healthcare and Sports information industry. Skilled in Java/J2EE, Spring Boot , Microservices, Hibernate, Spring Mvc , Web Services(REST, SOAP), Apache Camel , CI/CD,Jenkins, Test Driven Development (TDD) Junit, Behavior driven development (BDD) Cucumber and SQL.

Updated on July 09, 2022

Comments

  • Arun Kumar
    Arun Kumar almost 2 years

    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);
    }
    
  • Arun Kumar
    Arun Kumar over 12 years
    Stoffer, I have tried the way, you have answered. But whn i tried to POST any XML/JSON content in request body to controller method. i am getting the Exception from there i.e HTTP Status 400 - Content type 'text/plain;charset=UTF-8' not supported Is it because i am using both ContentNegotiatingViewResolver and Http Message Convertors?? or there is some other reason behind this.
  • Andreas Wederbrand
    Andreas Wederbrand over 12 years
    Converters in Spring register for certain content-type (for the request body) and accept (for the response). For json it's application/json and some others. For XML it's application/xml and some others. Make sure your client sends content-type: application/json and accept: application/json and it will work.
  • Arun Kumar
    Arun Kumar over 12 years
    @Andreas Wederbrand.... Thanks For your comment Andreas.. Its really helped mt the way i am thinking to POST JSON/XML contents to Controller.
  • Arun Kumar
    Arun Kumar over 12 years
    @Andreas Wederbrand....Now I am able To POST any of the JSON or XML Content to my Spring Controller with content-type: application/json for JSON and content-type: application/xml for XML..Thanks for your advice....Cheers...:)