【发布时间】:2019-04-09 17:59:09
【问题描述】:
我想在从数据库中获取 REST PATH 有效负载后将其合并到“实体”对象,这样只有有效负载中提供的属性才会在实体中更新。因此,我想确保只有作为补丁有效负载一部分提供的属性才能安全更新。
我正在将 Spring Rest Controller 与 Hibernate 实体一起使用。
@PatchMapping(value = "/{id}")
public Resource<DepartmentPEO> update(@PathVariable Long id,
@RequestBody JSONObject payload) throws Exception
{
DepartmentPEO eo = departmentService.getRow(id);
// Have to do something to update the eo object from jsonObject.
// Some api to update eo
eo = departmentService.update(id, eo);
Resource<DepartmentPEO> resource = new Resource<>(eo);
DepartmentPEO dept = resource.getContent();
id = dept.getDeptSeq();
resource.add(
linkTo(methodOn(DepartmentsRestController.class).getRow(id))
.withSelfRel());
return resource;
}
仅将修改后的属性作为有效负载的一部分发送到服务器,而不是发送所有属性。资源(实体)将具有嵌套的对象列表(一对多)。我正在为此用例寻找防池解决方案,并且相信这对于每个支持 REST api 的应用程序来说都是常见/基本的。
如果您指点任何 API 来解决这个问题,我们将不胜感激!
谢谢
【问题讨论】:
标签: java json hibernate rest spring-rest