我在Spring中有了这个PUT端点:
@PutMapping("{id}")
public SomeEntityDTO update(@PathVariable("id") String id, @RequestBody SomeEntityDTO entityDTO) {
entityDTO.setId(id);
Entity entity = entityMapper.map(entityDTO);
Entity updated = entityService.save(entity);
return entityDTOMapper.map(updated);
}
我想要一些映射库来实现这一点:
@PutMapping("{id}")
public SomeEntityDTO update(@PathVariable("id") String id, @RequestBody SomeEntityDTO entityDTO) {
Entity entity = entityMapper.map(id, entityDTO); // <---
Entity updated = entityService.save(entity);
return entityDTOMapper.map(updated);
}
是否有任何映射库支持实体/ DTO转换,同时考虑到DTO中没有的额外参数(如本例中的id
)?
我正在寻找“本地”的东西,以避免可怕的代码。
提前感谢!
发布于 2022-06-15 21:36:30
通常,我们在项目中使用MapStruct。使用MapStruct,您可以轻松地执行所需的映射操作。https://mapstruct.org/
https://stackoverflow.com/questions/72640747
复制相似问题