Announcement Announcement Module
Collapse
No announcement yet.
How do I post to a REST service using RestTemplate in Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How do I post to a REST service using RestTemplate in Spring

    How do I post to a REST service using RestTemplate. I have the following RESTful web service and I would like to use RestTemplate to post a new user object to to?

    Code:
        @ResponseBody
        @RequestMapping(value = "/user/{id}", method = RequestMethod.POST )
        public User updateCustomer(@PathVariable("id") Integer id, @RequestBody User user) {
        
            logger.debug("I am in the controller and got user name: " + user.toString());
            return user;
        }
    How do I change the following code to post the user object?

    Code:
        
           RestTemplate rt = new RestTemplate(commons);
                    rt.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
                    rt.getMessageConverters().add(new StringHttpMessageConverter());
        
                    URI uri = new URI("http://" + mRESTServer.getHost() + ":8080/springmvc-rest-secured-test/json/user/{id}");
        
                    User u = new User();
                    u.setName("Mike M Smith");
                    u.setUser("MMS");
        
                    String returns = rt.postForObject(uri, u, String.class);
        
                    LOGGER.debug("User:  " + u.toString());
    Last edited by techsjs; Jun 17th, 2013, 02:50 PM.
Working...
X