Spring Data REST custom query integration

筅森魡賤 提交于 2019-12-28 08:12:01

问题


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

So I have the following Repository

 @RepositoryRestResource(collectionResourceRel="employees", path="employees")
 public interface EmployeeRepository extends PagingAndSortingRepository<Employee, Long>, 
                                                         JpaSpecificationExecutor<Employee> {

 }

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);
}

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;

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.

EDIT: 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


回答1:


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);
    }
}

This works perfectly with Spring Data Rest 2.1.4.RELEASE




回答2:


The code by @Stackee007 works but the resource won't include self links. In order to do that, a little more is required.

@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);
}


来源:https://stackoverflow.com/questions/25980759/spring-data-rest-custom-query-integration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!