不要直接从客户端接收 a ,而是JsonPatch
定义一个 DTO 来处理验证,然后您稍后会将 DTO 实例转换为JsonPatch
.
假设您要更新 instance 的用户User.class
,您可以定义一个 DTO,例如:
public class UserDTO {
@Email(message = "The provided email is invalid")
private String username;
@Size(min = 2, max = 10, message = "firstname should have at least 2 and a maximum of 10 characters")
private String firstName;
@Size(min = 2, max = 10, message = "firstname should have at least 2 and a maximum of 10 characters")
private String lastName;
@Override
public String toString() {
return new Gson().toJson(this);
}
//getters and setters
}
自定义toString
方法确保更新请求中未包含的字段不会预填充null
值。
您的PATCH
要求可以如下(为简单起见,我没有迎合例外)
@PatchMapping("/{id}")
ResponseEntity<Object> updateUser(@RequestBody @Valid UserDTO request,
@PathVariable String id) throws ParseException, IOException, JsonPatchException {
User oldUser = userRepository.findById(id);
String detailsToUpdate = request.toString();
User newUser = applyPatchToUser(detailsToUpdate, oldUser);
userRepository.save(newUser);
return userService.updateUser(request, id);
}
以下方法返回上面在控制器中更新的修补用户。
private User applyPatchToUser(String detailsToUpdate, User oldUser) throws IOException, JsonPatchException {
ObjectMapper objectMapper = new ObjectMapper();
// Parse the patch to JsonNode
JsonNode patchNode = objectMapper.readTree(detailsToUpdate);
// Create the patch
JsonMergePatch patch = JsonMergePatch.fromJson(patchNode);
// Convert the original object to JsonNode
JsonNode originalObjNode = objectMapper.valueToTree(oldUser);
// Apply the patch
TreeNode patchedObjNode = patch.apply(originalObjNode);
// Convert the patched node to an updated obj
return objectMapper.treeToValue(patchedObjNode, User.class);
}