在我的应用中,我可以创建和更新实体。问题是我无法在一个createOrUpdate方法中将这两个方法与POST映射合并,并检查对象是否是新的(这是因为ID不是由用户提供的自动生成的)。 我最终制作了一个方法创建(POST映射)和更新(PUT映射)。但是过了一段时间我知道在Spring中,如果方法是PUT,则无法请求参数。 所以,我想我应该使用2种POST方法,但它们具有相同的URL模式,因为我的应用程序无法正常工作。
是否可以制作类似的东西?
@RequestMapping(value = "/ajax/users")
/.../
@PostMapping (//specific param here to distinguish???)
public void create(User user)
{
service.save(user);
}
@PostMapping(//specific param here to distinguish???)
public void update(User user)
{
service.update(user);
}
function save() {
$.ajax({
type: "POST", //specific param here to distinguish?
url: ajaxUrl,
data: form.serialize(),
success: function () {
$("#editRow").modal("hide");
updateTable();
successNoty("Saved");
}
});
}
function update() {
$.ajax({
type: "POST",//specific param here to distinguish?
url: ajaxUrl,
data: form.serialize(),
success: function () {
$("#editRow").modal("hide");
updateTable();
successNoty("Updated");
}
});
}
提前致谢
答案 0 :(得分:1)
您可以使用@RequestBody注释:
@PutMapping("/users/{id}")
public void update(@PathVariable Long id, @RequestBody User user)
{
service.update(user);
}
答案 1 :(得分:0)
这可能会对您有所帮助:
@RequestMapping(value="/users/{id}", method=RequestMethod.POST)