lundi 2 mars 2015

Java Spring, @RequestBody - how to receive valid JSON with two connected entities

I've got simple


DB SCHEMA



Contact
-------
int: id
varchar: name

Note
-------
int: id
int: id_contact
varchar: title


As you can see, tables are connected. My goal is to add note by AJAX request with id_contact provided.


FORM should look like that



<form action="add-note" id="add-note">
<input type="hidden" name="id_contact" value="20" /> <!-- just a dummy value which corresponds with contact record in DB -->

note:
<input type="text" name="title" class="title">

<input type="submit" />
</form>


Method to process AJAX request



@RequestMapping(value = "/add-note", method = RequestMethod.POST)
public ResponseEntity<Note> addNote(@RequestBody Note note) {

// do something with note...

return new ResponseEntity<Note>(note, HttpStatus.OK);
}


If I don't try to provide id_contact, method addNote works fine, but I'm not able to make spring to convert JSON to Note with Contact in it.


Does anyone know how to achieve it? Thanks in advance


Aucun commentaire:

Enregistrer un commentaire