上記のソリューションは、リクエストボディのPOJO(つまりPersonオブジェクト)への自動変換を克服するために、メソッドシグニチャを少し変更する必要があります。
方法1: -
代わりのPOJOクラス(人)にリクエストボディを変換するには、地図のようなオブジェクトを受け取り、キーの「名前」の存在を確認することができます。
@PatchMapping("/update")
public String updateOnlyIfFieldIsPresent1(@RequestBody Map<String, Object> requestBody) {
if (requestBody.get("name") != null) {
return "Success" + requestBody.get("name");
} else {
return "Success" + "name attribute not present in request body";
}
}
方法2: -
Stringとしてリクエストボディを受け取り、文字列(すなわち名前)を確認。
@PatchMapping("/update")
public String updateOnlyIfFieldIsPresent(@RequestBody String requestString) throws JsonParseException, JsonMappingException, IOException {
if (requestString.contains("\"name\"")) {
ObjectMapper mapper = new ObjectMapper();
Person person = mapper.readValue(requestString, Person.class);
return "Success -" + person.getName();
} else {
return "Success - " + "name attribute not present in request body";
}
}
は、私はそれが春のMVCの – DamCx
可能な重複によって同じように見られるように、あなたがほとんど知らないだろうと思います(HTTPS [春休憩コントローラーでの部分的な更新のためのnullではなく提供される値を区別する方法]:// howover-flowing.com/questions/38424383/how-to-distinguish-between-null-and-not-provided-values-for-partial-updates-in-s) – laffuste