Announcement Announcement Module
Collapse
No announcement yet.
RestTemplate POST JSON Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • RestTemplate POST JSON

    I am trying to post an object as JSON. I am using the RestTemplate for GET without any problems. What is woring with this setup?




    Code:
    RestTemplate restTemplate = new RestTemplate();
    List<HttpMessageConverter<?>> list = new ArrayList<HttpMessageConverter<?>>();
    list.add(new MappingJacksonHttpMessageConverter());
    restTemplate.setMessageConverters(list);
    		
    Person person = new Person(); 
    String url = "http://localhost:8080/add"; 
    Person response = restTemplate.postForObject(url, person, Person.class);



    Code:
    	@RequestMapping(value = "/add", method = RequestMethod.POST ,headers = "Accept=application/json,text/html,application/xhtml+xml,application/xml")
    public ModelAndView addPerson(@RequestBody Person person) {
    	return new ModelAndView("");
    }

    Code:
    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 404 Not Found
    	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:681)
    	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:574)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

  • #2
    Could somone please help? I have been googling and trying examples for 3 nights. I would like to make a post to a controller. Even postindustrielle a string would be ok. Many people have the same problem but this question is never answerd.

    Comment


    • #3
      Hi:
      You can try posting the object using HttpEntity, the code looks like below..


      RestTemplate restTemplate = new RestTemplate();
      List<HttpMessageConverter<?>> list = new ArrayList<HttpMessageConverter<?>>();
      list.add(new MappingJacksonHttpMessageConverter());
      restTemplate.setMessageConverters(list);
      Person person = new Person();
      String url = "http://localhost:8080/add";
      HttpEntity<Person> entity = new HttpEntity<Person>(person);

      ResponseEntity<Person> response = restTemplate.postForEntity(
      url,
      entity,
      Person.class);
      Person person = response.getBody();

      Regards,
      Sreedhar G

      Comment


      • #4
        Thank you !

        I got i working in another project with.
        @RequestMapping(value = "/add", method = RequestMethod.POST ,consumes = "application/json")

        Comment

        Working...
        X