REST経由でリソースを更新する場合、更新する値のみ、またはオブジェクト全体(現在の値と更新する値)を本文に含める必要がありますか?REST(PUT/POST)によるリソースの更新
ユーザーオブジェクトがこの
User (id, name, age, sex)
のように見えると私はだけ彼の名前と年齢を更新したい場合は、私の要求は次のようになります。
PUT /users/1
{"name":"john","age":18}
またはこのような:
PUT /users/1
{"name":"john","age":18, "sex":"m"}
サーバー上でどのように見えるのですか。イデア?
@RequestMapping(value = "/{userId}", method = PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> updateUser(@PathVariable final int userId, @RequestBody User u){
//fetch user by ID
user.setName(u.getName())
user.setAge(u.getAge())
user.setSex(u.getSex()) //this will be empty?
return new ResponseEntity<String>(gson.toJson(user), HttpStatus.OK);
}
または代わりに私がリクエストボディに含まれており、この
if(u.getName()!=null){
user.setName(u.getName())
}
if(u.getAge()!=null){
user.setAge(u.getAge())
}
if(u.getSex()!=null){
user.setSex(u.getSex())
}
ような何かをされなかった変数を見つけることができ、これを達成するために、右/間違った方法はありますか、それがそうです一番簡単なことをやっているの?