【发布时间】:2021-04-14 19:44:44
【问题描述】:
我们正在使用 Spring Boot 对实体进行部分更新,如下所示,是否有更优雅的方式,好像实体包含 50 多个属性,然后处理起来真的很痛苦
public Foo updateFoo(@PathVariable String id, @RequestBody Foo fooInput) {
Foo foo = fooRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Foo not found for this id: " + id));
if (fooInput.getCaption() != null) {
foo.setCaption(fooInput.getCaption());
}
if (fooInput.getBar() != null) {
foo.setBar(fooInput.getBar());
}
...
}
【问题讨论】:
-
也许你要找的是一个DTO-Model转换工具,你可以找到
modelmapperhere的介绍。
标签: spring spring-boot