gpt4 book ai didi

java - Spring MVC - Rest API 是将实体放在一起的最佳位置

转载 作者:行者123 更新时间:2023-11-29 04:10:55 25 4
gpt4 key购买 nike

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

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

@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!");
}
}

在 Controller 中,将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!");
}
}

最佳答案

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

@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!");
}
}

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

关于java - Spring MVC - Rest API 是将实体放在一起的最佳位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55216723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com