gpt4 book ai didi

Spring Data REST 自定义查询集成

转载 作者:IT老高 更新时间:2023-10-28 13:56:28 25 4
gpt4 key购买 nike

我想为 Employee 实体创建一个 REST 链接,该实体基本上是一个 findByAllFields 查询。当然这应该与 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 集成。我试图组装一个资源,将 Controller 更改为:

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” Controller 之外的所有 Controller 都在工作。 https://github.com/cgeo7/spring-rest-example

最佳答案

尝试将 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 完美配合

关于Spring Data REST 自定义查询集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25980759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com