Spring Data REST custom query integration
Asked Answered
V

3

13

I want to create a REST link for an Employee entity that will basically be a findByAllFields query. Of course this should be combined with Page and Sort. In order to do that I have implemented the following code:

@Entity
public class Employee extends Persistable<Long> {

    @Column
    private String firstName;

    @Column
    private String lastName;

    @Column
    private String age;

    @Column
    @Temporal(TemporalType.TIMESTAMP)
    private Date hiringDate;
}

So I would like to have lets say a query where I can do:

http://localhost:8080/myApp/employees/search/all?firstName=me&lastName=self&ageFrom=20&ageTo=30&hiringDateFrom=12234433235

So I have the following Repository

 @RepositoryRestResource(collectionResourceRel="employees", path="employees")
 public interface EmployeeRepository extends PagingAndSortingRepository<Employee, Long>, 
                                                         JpaSpecificationExecutor<Employee> {

 }

Ok so now I need a RestController

@RepositoryRestController
public class EmployeeSearchController {

    @Autowired
    private EmployeeRepository employeRepository;

    @RequestMapping(value = "/employees/search/all/search/all", method = RequestMethod.GET)
    public Page<Employee> getEmployees(EmployeeCriteria filterCriteria, Pageable pageable) {

        //EmployeeSpecification uses CriteriaAPI to form dynamic query with the fields from filterCriteria
        Specification<Employee> specification = new EmployeeSpecification(filterCriteria);

        return employeeRepository.findAll(specification, pageable);
}

Ok, obviously this does its job but it is not integrated with HATEOAS. I have attempted to assemble a resource changing the controller to this:

public PagedResources<Resource<Employee>> getEmployees(
                PagedResourcesAssembler<Employee> assembler,
                EmployeeCriteria filterCriteria, Pageable pageable) {

        //EmployeeSpecification uses CriteriaAPI to form dynamic query with the fields from filterCriteria
        Specification<Employee> specification = new EmployeeSpecification(filterCriteria);

        Page<Employee> employees = employeeRepository.findAll(specification, pageable);
        return assembler.toResource(employees);
}

Obviously I'm missing something from the above since it doesnt work and I'm getting the following Exception:

Could not instantiate bean class [org.springframework.data.web.PagedResourcesAssembler]: No default constructor found;

Ok so to make the question clear I am trying to integrate the above resource into the rest of the HATEOAS architecture. I'm not entirely sure if this is the correct approach so any other suggestions are welcome.

EDIT: Here you can see a similar implementation. Please take a look at the configuration, you will see that all but one of the "Person" controllers are working. https://github.com/cgeo7/spring-rest-example

Varico answered 22/9, 2014 at 18:24 Comment(3)
if you can create simple github project for your issue, it would be easier to find your issue... because there is no enough info - your configurations, dependencies and etcEyespot
I apologize the delay. I have created a minor similar project that reflects all the difficulties I have github.com/cgeo7/spring-rest-exampleVarico
@Varico and @Stackee007 , Do you mind terribly if I ask you how you define your EmployeeCriteria and EmployeeSpecification classes?Chrysoprase
S
18

Try autowring PagedResourcesAssembler as a class member and change method signature something like below

@RepositoryRestController
public class EmployeeSearchController {

    @Autowired
    private EmployeeRepository employeRepository;

    @Autowired
    private PagedResourcesAssembler<Employee> pagedAssembler;

    @RequestMapping(value = "/employees/search/all/search/all", method = RequestMethod.GET)
    public ResponseEntity<Resources<Resource<Employee>>> getEmployees(EmployeeCriteria filterCriteria, Pageable pageable) {

        //EmployeeSpecification uses CriteriaAPI to form dynamic query with the fields from filterCriteria
        Specification<Employee> specification = new EmployeeSpecification(filterCriteria);

        Page<Employee> employees = employeeRepository.findAll(specification, pageable);
        return assembler.toResource(employees);
    }
}

This works perfectly with Spring Data Rest 2.1.4.RELEASE

Steelwork answered 29/9, 2014 at 16:8 Comment(3)
See also #31759362Upgrowth
assembler variable is not defined!! Why?Swaney
@IvanSmorodin it's should be pagedAssembler.Diu
C
6

The code by @Stackee007 works but the resource won't include self links. In order to do that, a little more is required.

@Autowired
PagedResourcesAssembler<Appointment> pagedResourcesAssembler;

@RequestMapping(value = "/findTodaysSchedule")
public HttpEntity<PagedResources<Resource<Appointment>>> getTodaysSchedule(
        PersistentEntityResourceAssembler entityAssembler, Pageable pageable) {
    Page<Appointment> todaysSchedule = apptRepo.findByStartTimeBetween(beginningOfDay, endOfDay, pageable);

    @SuppressWarnings({ "unchecked", "rawtypes" })
    PagedResources<Resource<Appointment>> resource = pagedResourcesAssembler.toResource(todaysSchedule,
                (ResourceAssembler) entityAssembler);

    return new ResponseEntity<>(resource, HttpStatus.OK);
}
Casement answered 20/10, 2015 at 5:28 Comment(1)
Thanks for the added clarification. This works nicely!Ferroconcrete
F
0

Spring HATEOAS has changed the name of Resource, PagedResources and some other classes. See here. Below is a working version in 2020.

@RepositoryRestController
public class EmployeeSearchController {
    @Autowired
    private EmployeeRepository employeRepository;

    @Autowired
    private PagedResourcesAssembler<Employee> pagedAssembler;

    @RequestMapping(value = "/employees/search/all", method = RequestMethod.GET)
    public ResponseEntity<PagedModel<EntityModel<Employee>>> getEmployees(PersistentEntityResourceAssembler entityAssembler,,
                                                                          EmployeeCriteria filterCriteria,
                                                                          Pageable pageable) {

        Specification<Employee> specification = new EmployeeSpecification(filterCriteria);

        Page<Employee> employees = employeeRepository.findAll(specification, pageable);
        return ResponseEntity.ok(pagedAssembler.toModel(plants, (RepresentationModelAssembler) entityAssembler));
    }
}
Furst answered 11/7, 2020 at 5:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.