0

I am trying to save a domain object via my REST api from within another Spring MVC application using RestTemplate. It successfully makes the call to the api, but the foo and bar fields of TheThing object end up being null once it reaches the rest controller

from TheThingController in the MVC app

@RequestMapping(path = "/", method = RequestMethod.POST)
public String save(Model model) {
    TheThing theThing = new TheThing();
    theThing.setFoo("foo");
    theThing.setBar("bar");

    RestTemplate restTemplate = new RestTemplate();
    TheThing savedThing = restTemplate.postForObject("http://localhost:8090/theThing", theThing, TheThing.class);
    return "thingForm";
}

TheThingRestController

@RestController
public class TheThingRestController {

    @Autowired
    private TheThingMapper theThingMapper; 

    @RequestMapping(path = "/theThing", method = RequestMethod.POST)
    public void create(TheThing theThing) {
        theThingMapper.createTheThing(theThing); // theThing isn't null, but all of its fields are
    }
}

Somehow the fields of TheThing object are getting lost in translation.

secondbreakfast
  • 4,194
  • 5
  • 47
  • 101

1 Answers1

0

I figured it out. Just needed to Add the @RequestBody annotation to TheThing method parameter in the RestController

secondbreakfast
  • 4,194
  • 5
  • 47
  • 101