繁体   English   中英

使用spring数据jpa的spring boot。 如何从请求主体执行实体的部分更新?

[英]Spring boot with spring data jpa. How can i perform partial update of entity from request body?

基本上,我有带有更新方法的REST控制器类。

 @RequestMapping(value = "{id}", method = RequestMethod.POST)
public Account update(@PathVariable Integer id, @RequestBody Account account){
     .....
}

当请求正文仅包含Account对象的部分字段时,如何执行给定ID的Account对象的部分更新?

您能通过身份证获取吗? 拥有原始版本后,如果定义了属性,则可以使用新对象进行更新。 就像是:

@RequestMapping(value = "{id}", method = RequestMethod.POST)
public Account update(@PathVariable Integer id, @RequestBody Account account){
    Account acc = dao.getById(id);
    acc.copy(account);
    dao.update(acc);
}

class Account {
     ....
    public void copy(final Account account) {
         if (account.prop1 != null) {
             this.prop1 = account.prop1;
         }
         // update the mutable fields
         .....
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM