Spring Data REST自定义查询集成 [英] Spring Data REST custom query integration
问题描述
我想为Employee
实体创建一个REST链接,该链接基本上是一个findByAllFields
查询.当然,应将其与Page
和Sort
结合使用.为此,我实现了以下代码:
I want to create a REST link for an Employee
entity that will basically be a findByAllFields
query. Of course this should be combined with Page
and Sort
. In order to do that I have implemented the following code:
@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;
}
所以我想说一个我可以做的查询:
So I would like to have lets say a query where I can do:
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
Ok so now I need a 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集成. 我尝试组装一个将控制器更改为此的资源:
Ok, obviously this does its job but it is not integrated with HATEOAS. I have attempted to assemble a resource changing the controller to this:
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);
}
很明显,我遗漏了上面的内容,因为它不起作用,并且出现了以下异常:
Obviously I'm missing something from the above since it doesnt work and I'm getting the following Exception:
Could not instantiate bean class [org.springframework.data.web.PagedResourcesAssembler]: No default constructor found;
好的,为了使问题更明确我正在尝试将上述资源集成到HATEOAS架构的其余部分中.我不确定这是否是正确的方法,因此是否有其他建议欢迎.
Ok so to make the question clear I am trying to integrate the above resource into the rest of the HATEOAS architecture. I'm not entirely sure if this is the correct approach so any other suggestions are welcome.
在这里,您可以看到类似的实现.请看一下配置,您将看到除一个"Person"控制器之外的所有控制器都在工作. https://github.com/cgeo7/spring-rest-example
Here you can see a similar implementation. Please take a look at the configuration, you will see that all but one of the "Person" controllers are working. https://github.com/cgeo7/spring-rest-example
推荐答案
尝试自动将PagedResourcesAssembler
作为类成员,并更改方法签名,如下所示:
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);
}
}
这与Spring Data Rest 2.1.4.RELEASE
这篇关于Spring Data REST自定义查询集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!