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

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 etc Commented Sep 24, 2014 at 20:32
  • I apologize the delay. I have created a minor similar project that reflects all the difficulties I have github.com/cgeo7/spring-rest-example Commented Sep 25, 2014 at 17:06
  • 7
    @ChrisGeo and @Stackee007 , Do you mind terribly if I ask you how you define your EmployeeCriteria and EmployeeSpecification classes? Commented Oct 29, 2014 at 18:40

3 Answers 3

18
+50

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

Sign up to request clarification or add additional context in comments.

3 Comments

assembler variable is not defined!! Why?
@IvanSmorodin it's should be pagedAssembler.
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);
}

1 Comment

Thanks for the added clarification. This works nicely!
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));
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.