Reputation: 11
In my app I can create and update entities. The problem is I can not merge this 2 methods in one createOrUpdate method with POST mapping and check whether object is new or not(That's because ID is not auto-generated, provided by a user). I ended up with making one of the methods create(POST mapping) and update(PUT mapping). But after a while I knew that in Spring the one is not capable of requesting parameters, if the method is PUT. So, I guess I should use 2 POST methods for that, but they have the same URL pattern, since that my app can not work properly.
Is it possible to make something like that?
@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");
}
});
}
Thanks in advance
Upvotes: 0
Views: 775
Reputation: 1
This may help you:
@RequestMapping(value="/users/{id}", method=RequestMethod.POST)
Upvotes: 0
Reputation: 1015
You can use @RequestBody annotation:
@PutMapping("/users/{id}")
public void update(@PathVariable Long id, @RequestBody User user)
{
service.update(user);
}
Upvotes: 1