[英]Java Spring, @RequestBody - how to receive valid JSON with two connected entities
I've got simple 我很简单
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.
我的目标是通过提供ID_contact的AJAX请求添加注释。
<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>
@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. 如果我不尝试提供id_contact,方法addNote可以正常工作,但是我无法使spring能够将JSON转换为带有Contact的Note。
Does anyone know how to achieve it? 有人知道如何实现吗? Thanks in advance
提前致谢
EDIT: After all, I ended up with passing id_contact as GET param and handling it like that: 编辑:毕竟,我最终以id_contact作为GET参数传递并像这样处理它:
@RequestMapping(value = "/addNote", method = RequestMethod.POST)
public ResponseEntity<Note> addNote(@RequestBody Note note, @RequestParam(value = "id_contact") long id_contact) {
note.setContact(contact_service.findOne(id_contact));
note_service.insert(note);
return new ResponseEntity<Note>(note, HttpStatus.OK);
}
Feel free to answer to my question above - I consider this snippet as a little workaround. 请随意回答我的上述问题-我认为此代码段是一种解决方法。 (But maybe it's the right way how you would do it by urself?)
(但是也许这是正确的方法,自己做吧?)
EDIT2: EDIT2:
// imports ommited
@Entity
public class Note {
@Id
@JsonProperty("url")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
@ManyToOne
@JoinColumn(name="id_contact")
private Contact contact;
private String title;
// setters/getters ommited
Error I get when trying to send JSON is "400 bad request". 尝试发送JSON时收到的错误是“ 400错误请求”。
Not sure if this is going to work but try to use dot operator. 不知道这是否行得通,但尝试使用点运算符。
<input type="hidden" name="contact.id" value="20" />
<input type="hidden" name="contact.name" value="20" />
RequestBody will try to map the notes Entity, and using nested objects sometimes works. RequestBody将尝试映射注释Entity,并且有时可以使用嵌套对象。
If not considering creating a json request instead of use the multpart form format. 如果不考虑创建json请求而不是使用multpart表单格式。
Neither of your examples from your comments is valid JSON. 您的注释中的两个示例都不是有效的JSON。 I believe
{"title": "test", "contact" : {"id" : 20}}
would work for you in a sense that it will be parsed by Spring properly into a Note
. 我相信
{"title": "test", "contact" : {"id" : 20}}
在某种意义上将对您有用,Spring会将其正确解析为Note
。 However your form won't produce any JSON, and I can't see any AJAX calls anywhere in your post. 但是,您的表单不会产生任何JSON,并且您的帖子中的任何地方都看不到任何AJAX调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.