Spring Data REST custom query integration

前端 未结 3 989
礼貌的吻别
礼貌的吻别 2020-12-07 19:14

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 Pa

相关标签:
3条回答
  • 2020-12-07 19:59

    Spring HATEOAS has changed the name of Resource, PagedResources and some other classes. See here. Below is a working version in 2020.

    @RepositoryRestController
    public class EmployeeSearchController {
        @Autowired
        private EmployeeRepository employeRepository;
    
        @Autowired
        private PagedResourcesAssembler<Employee> pagedAssembler;
    
        @RequestMapping(value = "/employees/search/all", method = RequestMethod.GET)
        public ResponseEntity<PagedModel<EntityModel<Employee>>> getEmployees(PersistentEntityResourceAssembler entityAssembler,,
                                                                              EmployeeCriteria filterCriteria,
                                                                              Pageable pageable) {
    
            Specification<Employee> specification = new EmployeeSpecification(filterCriteria);
    
            Page<Employee> employees = employeeRepository.findAll(specification, pageable);
            return ResponseEntity.ok(pagedAssembler.toModel(plants, (RepresentationModelAssembler) entityAssembler));
        }
    }
    
    0 讨论(0)
  • 2020-12-07 20:03

    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);
    }
    
    0 讨论(0)
  • 2020-12-07 20:11

    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

    0 讨论(0)
提交回复
热议问题