Answer the question
In order to leave comments, you need to log in
Spring MVC: how to properly update a model?
Hello! I'm learning Spring. The question arose how to update the model correctly?
What you need: there is a Pet(id, ownerId, name, ....) model. The user can edit some fields, some cannot (for example, cannot edit ownerId). You need to get data from the client, update those fields that can be updated and saved.
The controller method looks like this so far:
@RequestMapping(value = "/pet/{id}", method = RequestMethod.PUT)
@ResponseBody
public Pet updatePet(@PathVariable int id, @RequestBody Pet pet) {
.....
}
Answer the question
In order to leave comments, you need to log in
There is a form binding
@RequestMapping(value = "/pet/update", method = RequestMethod.PUT)
@ResponseBody
public Pet updatePet(@Valid @ModelAttribute Pet pet) {
...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question