Search code examples
springspring-mvcspring-3

RestTemplate post for entity


My post method gets called but my Profile is empty. What is wrong with this approach? Must I use @Requestbody to use the RestTemplate?

Profile profile = new Profile();
profile.setEmail(email);        
String response = restTemplate.postForObject("http://localhost:8080/user/", profile, String.class);


@RequestMapping(value = "/", method = RequestMethod.POST)
    public @ResponseBody
    Object postUser(@Valid Profile profile, BindingResult bindingResult, HttpServletResponse response) {

    //Profile is null
        return profile;
    }

Solution

  • You have to build the profile object this way

    MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
    parts.add("email", email);
    
    Object response = restTemplate.postForObject("http://localhost:8080/user/", parts, String.class);