【发布时间】:2020-06-19 09:49:45
【问题描述】:
我正在创建 Spring Boot HATEOAS REST 应用程序。下面的代码显示了我如何添加链接,同时为特定员工发送 GET 请求。我正在使用RepresentationModelAssemblertoModel 函数。还有toCollectionModel 函数可以覆盖,我想用它来将List<Employees> 转换为CollectionModel。 -> 这将在 /Employees/all 端点中返回。
我不知道该怎么做。 所以我需要传递List<Employees>,然后所有列表元素都需要由toModel函数处理,然后,就像在toModel函数中一样,我需要向它添加更多链接的可能性->链接到整个新集合(不是单个项目)。
期待您的回答!
@Component
public class EmployeeModelAssembler implements RepresentationModelAssembler<Employee, EntityModel<Employee>> {
@Override
public EntityModel<Employee> toModel(Employee employee) {
EntityModel<Employee> employeeEntityModel = EntityModel.of(employee);
Link selfLink = linkTo(methodOn(EmployeeController.class).getEmployeeById(employee.getId())).withSelfRel();
employeeEntityModel.add(selfLink);
return employeeEntityModel;
}
@Override
public CollectionModel<EntityModel<Employee>> toCollectionModel(Iterable<? extends Employee> entities) {
?? ?? ??
}
}
【问题讨论】:
标签: spring spring-boot rest spring-hateoas hateoas