【问题标题】:Spring MVC - Rest API where is the best place for put entity togetherSpring MVC - 将实体放在一起的最佳位置是 Rest API
【发布时间】:2019-03-18 07:51:14
【问题描述】:

设置父实体的最佳位置在哪里?换句话说,将实体放在一起的最佳位置在哪里?

在控制器中,找到父实体并设置为子实体然后保存?

@RestController
public class SomeController{

    @Autowired
    private SomeService someService;

    @PostMapping("/parents/{parentEntityId}/childs")
    public ResponseEntity<Void> save(@PathVariable("parentEntityId") Long parentEntityId, @RequestBody ChildDto childDto) {
        Optional<ParentEntity> parentEntity = someService.findParentById(parentEntityId);
        if (parentEntity.isPresent()) {
            ChildEntity childEntity = childDtoMapper.fromDto(childDto);
            childEntity.setParent(parentEntity.get());
            someService.saveChild(childEntity);
            return ResponseEntity.created(...).build();
        }
        throw new EntityNotFoundException("Parent entity not found!");
    }
}

在控制器中,将 dto 映射到实体,然后将实体和父实体 id 发送到服务,然后通过 id 找到父实体并设置为子实体并保存?

@RestController
public class SomeController {

    @Autowired
    private SomeService someService;

    @PostMapping("/parents/{parentEntityId}/childs")
    public ResponseEntity<Void> save(@PathVariable("parentEntityId") Long parentEntityId, @RequestBody ChildDto childDto) {
            ChildEntity childEntity = childDtoMapper.fromDto(childDto);
            someService.saveChild(parentEntityId, childEntity);
            return ResponseEntity.created(...).build();
    }
}

public class SomeServiceImpl {

    @Autowired
    private ParentEntityRepository parentEntityRepository;

    @Autowired
    private ChildEntityRepository childEntityRepository;

    public ChildEntity saveChild(final long parentEntityId, final ChildEntity childEntity){
        Optional<ParentEntity> parentEntity = parentEntityRepository.findById(parentEntityId);
        if (parentEntity.isPresent()) {
            childEntity.setParent(parentEntity.get());
            childEntityRepository.save(childEntity);
            return childEntity;
        }
        throw new EntityNotFoundException("Parent entity not found!");
    }
}

【问题讨论】:

    标签: java spring rest spring-mvc


    【解决方案1】:

    我会选择第二个选项并进行以下修改:

    @RestController
    public class SomeController {
    
        @Autowired
        private SomeService someService;
    
        @PostMapping("/parents/{parentEntityId}/childs")
        public ResponseEntity<Void> save(@PathVariable("parentEntityId") Long parentEntityId, @RequestBody ChildDto childDto) {
                someService.saveChild(parentEntityId, childDto);
                return ResponseEntity.created(...).build();
        }
    }
    
    public class SomeServiceImpl {
    
        @Autowired
        private ParentEntityRepository parentEntityRepository;
    
        @Autowired
        private ChildEntityRepository childEntityRepository;
    
        public ChildEntity saveChild(final long parentEntityId, final ChildDto childDto){
            ChildEntity childEntity = childDtoMapper.fromDto(childDto);
            Optional<ParentEntity> parentEntity = parentEntityRepository.findById(parentEntityId);
            if (parentEntity.isPresent()) {
                childEntity.setParent(parentEntity.get());
                childEntityRepository.save(childEntity);
                return childEntity;
            }
            throw new EntityNotFoundException("Parent entity not found!");
        }
    }
    

    考虑到这种方式,控制器几乎没有逻辑:它只是将请求转发到适当的服务,就像流量控制器一样。此外,使用这种方法,在未来的某一天,您必须添加一些“替代”控制器,例如 WebSocket 或旧式 WebService,您为此付出的努力将是最小的,并且您将最大限度地减少此控制器与任何未来之间的相似性控制器。 这种方法的黄金法则如下:DAO/Repository 总是返回 Entities,Service 总是返回 DTO。控制器只是外部世界的接口。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 2013-10-23
    • 2015-09-11
    • 2023-03-18
    • 2017-11-13
    相关资源
    最近更新 更多