Spring Data REST自定义查询集成

时间:2014-09-22 18:24:48

标签: spring rest spring-data-rest spring-hateoas

我想为Employee实体创建一个基本上是findByAllFields查询的REST链接。当然,这应与PageSort结合使用。为此,我实现了以下代码:

@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;
}

所以我想让我们说一下我可以做的查询:

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

所以我有以下Repository

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

 }

好的,现在我需要一个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);
}

好的,显然这可以完成它的工作,但它没有与HATEOAS集成。 我试图组装一个资源,将控制器更改为:

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);
}

显然我错过了上面的内容,因为它不起作用,我得到以下例外:

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

好的,这样可以清楚地解决问题我正在尝试将上述资源集成到HATEOAS架构的其余部分。我不完全确定这是否是正确的方法,因此任何其他建议都是欢迎。

编辑: 在这里,您可以看到类似的实现。请查看配置,您将看到除“一个”控制器之外的所有控制器都正常工作。 https://github.com/cgeo7/spring-rest-example

3 个答案:

答案 0 :(得分:16)

尝试将PagedResourcesAssembler作为类成员自动发送,并更改方法签名,如下所示

@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);
    }
}

这与Spring Data Rest 2.1.4.RELEASE

完美配合

答案 1 :(得分:6)

@ Stackee007的代码可以使用,但资源不会包含self个链接。为了做到这一点,需要更多。

@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);
}

答案 2 :(得分:0)

Spring HATEOAS更改了Resource,PagedResources和其他一些类的名称。参见here。以下是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));
    }
}