Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring HATEOAS表示模型汇编程序到集合模型()_Spring_Spring Boot_Rest_Spring Hateoas_Hateoas - Fatal编程技术网

Spring HATEOAS表示模型汇编程序到集合模型()

Spring HATEOAS表示模型汇编程序到集合模型(),spring,spring-boot,rest,spring-hateoas,hateoas,Spring,Spring Boot,Rest,Spring Hateoas,Hateoas,我正在创建SpringBootHateoasREST应用程序。下面的代码显示了如何添加链接,而GET请求是针对特定员工发送的。我正在使用representationmodelsassemblertoModel函数。还有要覆盖的toCollectionModel函数,我想用它将List转换为CollectionModel。->这将在/Employees/all端点中返回 我不知道该怎么做所以我需要的是传递列表,然后所有列表元素都需要由toModel函数处理,然后,像在toModel函数中一样,我需

我正在创建SpringBootHateoasREST应用程序。下面的代码显示了如何添加链接,而GET请求是针对特定员工发送的。我正在使用
representationmodelsassembler
toModel
函数。还有要覆盖的
toCollectionModel
函数,我想用它将
List
转换为CollectionModel。->这将在/Employees/all端点中返回

我不知道该怎么做所以我需要的是传递
列表
,然后所有列表元素都需要由
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) {

        ?? ?? ??

    }
}
@组件
公共类EmployeeModelAssembler实现了RepresentationModelAssembler{
@凌驾
公共实体模型toModel(员工){
EntityModel employeeEntityModel=EntityModel.of(员工);
Link selfLink=linkTo(methodOn(EmployeeController.class).getEmployeeById(employee.getId()).withSelfRel();
employeeEntityModel.add(自链接);
返回employeeEntityModel;
}
@凌驾

public CollectionModel到CollectionModel(Iterable您可以使用以下内容:

@GetMapping(produces = { "application/hal+json" })
public CollectionModel<Customer> getAllCustomers() {
    List<Customer> allCustomers = customerService.allCustomers();
 
    for (Customer customer : allCustomers) {
        String customerId = customer.getCustomerId();
        Link selfLink = linkTo(CustomerController.class).slash(customerId).withSelfRel();
        customer.add(selfLink);
        if (orderService.getAllOrdersForCustomer(customerId).size() > 0) {
            Link ordersLink = linkTo(methodOn(CustomerController.class)
              .getOrdersForCustomer(customerId)).withRel("allOrders");
            customer.add(ordersLink);
        }
    }
 
    Link link = linkTo(CustomerController.class).withSelfRel();
    CollectionModel<Customer> result = CollectionModel.of(allCustomers, link);
    return result;
}
@GetMapping(生成={“应用程序/hal+json”})
公共集合模型getAllCustomers(){
列出所有客户=customerService.allCustomers();
for(客户:所有客户){
字符串customerId=customer.getCustomerId();
Link selfLink=linkTo(CustomerController.class).slash(customerId).withSelfRel();
customer.add(selfLink);
if(orderService.getAllOrdersForCustomer(customerId).size()>0){
Link ordersLink=linkTo(methodOn(CustomerController.class)
.getOrdersForCustomer(customerId)).withRel(“allOrders”);
customer.add(ordersLink);
}
}
Link=linkTo(CustomerController.class).withSelfRel();
CollectionModel结果=CollectionModel.of(所有客户,链接);
返回结果;
}
详细解释请访问