我想爲Employee
實體創建REST鏈接,該實體基本上是findByAllFields
查詢。當然這應該與Page
和Sort
相結合。爲了做到這一點我已經實現了下面的代碼:Spring Data REST自定義查詢集成
@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
,如果你能爲你的問題簡單github上的項目,它會更容易找到您的問題......因爲沒有足夠的信息 - 你的配置,依賴和等 – 2014-09-24 20:32:06
我道歉的延遲。我創建了一個類似的小項目,反映了我擁有的所有困難https://github.com/cgeo7/spring-rest-example – ChrisGeo 2014-09-25 17:06:20
@ChrisGeo和@ Stackee007,如果我問你如何定義你的EmployeeCriteria,你介意嗎? '和'EmployeeSpecification'類? – BalRog 2014-10-29 18:40:56