M
M
mitaichik2017-09-20 00:53:34
Spring
mitaichik, 2017-09-20 00:53:34

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) {
           .....
  }

I found an example of what to write there https://stackoverflow.com/questions/15313290/sprin...
But is there really no better solution in Spring for these purposes than stupid field assignment?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2017-09-20
@mitaichik

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 question

Ask a Question

731 491 924 answers to any question