Reputation: 1555
I found a similar problem here and tried to fix it, but it still does not work.
Using: Hibernate, Spring MVC
I have a @ManyToMany relationship between the entities Traveler and Address.
If I create the Traveler and Address individually by their own controller, it works. But if I try to create a Traveler and the appropriate Address by the following controller, I get a 400 Bad request error:
@RequestMapping(value = "/travelers", method=RequestMethod.POST)
public @ResponseBody Traveler createTraveler(@RequestBody Traveler traveler, Address address) {
logger.info("Start createTraveler");
System.out.println("Received traveler: " + traveler.getLastName());
travelerDAO.save(traveler);
System.out.println("Received address: " + address.getStreet());
addressDAO.save(address);
logger.info("End createTraveler");
return traveler;
}
Upvotes: 1
Views: 791
Reputation: 24561
If your JSON payload is traveler object with nested addresses, spring should fill in collection of addresses for Traveler automatically (with help of Jackson). Try it without address parameter.
@RequestMapping(value = "/travelers", method=RequestMethod.POST)
public @ResponseBody Traveler createTraveler(@RequestBody Traveler traveler) {
logger.info("Start createTraveler");
System.out.println("Received traveler: " + traveler.getLastName());
travelerDAO.save(traveler);
System.out.println("Received addresses: " + traveler.getAddresses());
//save addresses in loop
return traveler;
Upvotes: 1