2014-09-22 106 views
12

我想爲Employee實體創建REST鏈接,該實體基本上是findByAllFields查詢。當然這應該與PageSort相結合。爲了做到這一點我已經實現了下面的代碼: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

+0

,如果你能爲你的問題簡單github上的項目,它會更容易找到您的問題......因爲沒有足夠的信息 - 你的配置,依賴和等 – 2014-09-24 20:32:06

+0

我道歉的延遲。我創建了一個類似的小項目,反映了我擁有的所有困難https://github.com/cgeo7/spring-rest-example – ChrisGeo 2014-09-25 17:06:20

+7

@ChrisGeo和@ Stackee007,如果我問你如何定義你的EmployeeCriteria,你介意嗎? '和'EmployeeSpecification'類? – BalRog 2014-10-29 18:40:56

回答

15

嘗試autowring 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); 
    } 
} 

這由@ Stackee007工作,但與Spring數據休息2.1.4.RELEASE

+0

另請參閱http://stackoverflow.com/questions/31758862/enable-hal-serialization-in-spring-boot-for-custom-controller-method#31782016 – 2015-10-11 00:51:15

+1

彙編變量未定義!爲什麼? – 2015-11-11 11:40:50

6

的代碼完全工作該資源將不包括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); 
} 
+0

感謝您的補充說明。這很好用! – Jon 2016-03-25 14:23:48