我想知道如何分离REST API地址以下载有关电影的数据。目前,我的假设是为每种类型的信息提供单独的地址。
/api/v1.0/movies/contributions/othertitles/{id}
/api/v1.0/movies/contributions/boxoffice/{id}
/api/v1.0/movies/contributions/releasedates/{id}
e.g.
此控制器仅接受贡献ID
@GetMapping(value = "/contributions/othertitles/{id}")
@ResponseStatus(HttpStatus.OK)
public
Contribution<OtherTitle> getOtherTitleContribution(
@PathVariable final Long id
) {
return this.contributionSearchService.getOtherTitleContribution(id);
}
但是,我想让控制器更具普及性。
@GetMapping(value = "/contributions/{id}")
@ResponseStatus(HttpStatus.OK)
public
Contribution<? extends MovieRequest> getContribution(
@PathVariable final Long id,
@PathVariable final MovieField field
) {
return this.contributionSearchService.getContribution(id, field);
}
只有这样才能检索贡献的服务方法存在问题。例如,它看起来像这样。
@Override
public Contribution<OtherTitle> getOtherTitleContribution(
Long contributionId
) {
final ContributionEntity contributionEntity = this.findContribution(contributionId);
final Contribution<OtherTitle> contribution = contributionEntity.getDTO();
contributionEntity.getIdsToAdd()
.forEach(id -> {
final MovieOtherTitle otherTitle = this.entityManager.find(MovieOtherTitle.class, id);
contribution.getElementsToAdd().put(otherTitle.getId(), otherTitle.getDTO());
});
contributionEntity.getIdsToUpdate()
.forEach((key, value) -> {
final MovieOtherTitle otherTitleToUpdate = this.entityManager.find(MovieOtherTitle.class, key);
final MovieOtherTitle otherTitleUpdated = this.entityManager.find(MovieOtherTitle.class, value);
contribution.getElementsToUpdate().put(otherTitleToUpdate.getId(), otherTitleToUpdate.getDTO());
contribution.getElementsUpdated().put(otherTitleToUpdate.getId(), otherTitleUpdated.getDTO());
});
contributionEntity.getIdsToDelete()
.forEach(id -> {
final MovieOtherTitle otherTitle = this.entityManager.find(MovieOtherTitle.class, id);
contribution.getElementsToDelete().put(otherTitle.getId(), otherTitle.getDTO());
});
return contribution;
}
如果我想将此对象从控制器传递到站点并对其进行操作,我想下载哪些信息,那将是很多问题。
在我看来,最优化的是将其保留为现在的状态,并将其拆分为多个地址和服务中的几种方法。你觉得怎么样?