Spring Data REST custom query integration

我的未来我决定 提交于 2019-11-28 04:12:09

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

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);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!