我想为一个Employee
基本上是findByAllFields
查询的实体创建一个 REST 链接。当然,这应该与Page
and结合使用Sort
。为此,我实现了以下代码:
@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 架构的其余部分中。我不完全确定这是否是正确的方法,所以欢迎任何其他建议。
编辑:在这里你可以看到一个类似的实现。请看一下配置,你会看到除了一个“Person”控制器之外的所有控制器都在工作。 https://github.com/cgeo7/spring-rest-example