0

I have an entity class "Employee", Spring Data Rest returns all employees when I use a Get request on the path "../employees". By default it creates also links to @OneToMany relations. For example

        "_links": {
                "self": {
                    "href": "http://localhost:9000/mep/api/employees/1003"
                },
                "employee": {
                    "href": "http://localhost:9000/mep/api/employees/1003"
                },
                "assignmentProjectEmployeeSet": {
                    "href": "http://localhost:9000/mep/api/employees/1003/assignmentProjectEmployeeSet"
                }
            }

Is a part of the output. Now I filter this list with specifications in the follwing class.

  @Service
  public class EmployeeFilterService {
   public Resources<Employee> getFilteredEmployees(Employee toBeFilteredEmployee) {

    List<Employee> filteredEmployees = employeeRepository.findAll(

          // filter stuff

    );

    List<Resource<Employee>> filteredEmployeesResources = new ArrayList<>();
    for (Employee e : filteredEmployees) {
        Resource<Employee> resource = new Resource<>(e);                      
        filteredEmployeesResources.add(resource);
    }
    return new Resources(filteredEmployeesResources);
   }
 }

Unfortunately, this return no links. I know, that I can add links manually, but Spring Data Rest is able to create the missing links. How can I use this default behaviour?

I read something about PageResources, but I am not sure if this is the solution.

Thanks for any help Matthias

  • You have created custom controller/service so you will have to assemble the resources. You can look at using the in-built QueryDSL support to have Spring Data Rest handle the filtering (i.e. no custom controller or service) and you would get the desired response by appending query params e.g. `/employees?surname=smith` – Alan Hay Oct 26 '18 at 14:00

2 Answers2

1

A college was able to help me. I was looking for the following code:

public Resources<PersistentEntityResource> filterEmployee(@RequestParam Map<String, String> requestParam, PersistentEntityResourceAssembler assembler) {
    List<Employee> filteredList = employeeFilterService.getFilteredEmployees(requestParam);

    List<PersistentEntityResource> resourceList = new ArrayList<>();
    for (Employee e : filteredList) {
        resourceList.add(assembler.toResource(e));
    }
    return new Resources<>(resourceList);
}

The PersistentEntityResourceAssembler is the key and the class needs the annotation

@RepositoryRestController

With this the desired links are created.

Matthias

0

A snippet that helps - Detection strategy:

Detection strategy

So as per DEFAULT strategy, you can try to add a repository class/interface with a custom filter method, which would also include in link.

Karthik R
  • 5,523
  • 2
  • 18
  • 30
  • Hello Karthik, i am not sure if I understand it right, but my problem is not adding a new link for one of my method. I get a list of Employees and I want that Spring Data Rest add all links which would be created when I call the default URL .../employees –  Oct 26 '18 at 10:41