我真的经历了很多链接,例如:Set default page size for JPA Pageable Object,花了数周的时间来确定解决此问题的方法。
我已经开发了 Spring Data Jpa分页 示例。
Employee.java
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Employee implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="EMPLOYEE_ID")
private Long employeeId;
@Column(name="FIRST_NAME")
private String firstName;
@Column(name="LAST_NAME")
private String lastName;
@Column(name="EMAIL_ID")
private String email;
@Column(name="STATUS")
private String status;
}
EmployeeRepository.java
public interface EmployeeRepository extends JpaRepository<Employee, Long>{
Page<Employee> findByStatusOrderByFirstNameAsc(String status, Pageable pageable);
}
EmployeeService.java
public interface EmployeeService {
public Page<Employee> findAllEmployees(Pageable pr, Integer page, Integer size, String sortParam, String dir);
}
EmployeeServiceImpl.java
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
@Override
public Page<Employee> findAllEmployees(Pageable pr, Integer page, Integer size, String sortParam, String dir) {
Pageable pageable = null;
if (page != null && size != null && sortParam != null && dir != null) {
if (dir.equals("ASC"))
pageable = PageRequest.of(page, size, Sort.by(sortParam).ascending());
else if (dir.equals("DESC"))
pageable = PageRequest.of(page, size, Sort.by(sortParam).descending());
}else {
pageable = pr;
}
return employeeRepository.findByStatusOrderByFirstNameAsc("A", pageable);
}
}
EmployeeController.java
@RestController
@Api(tags = "Employees APIs")
@RequestMapping(path = "/employees")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@ApiOperation(value = "Get Employees By Pagination", nickname = "Find Employees")
@ApiResponses(value = { @ApiResponse(code = 200, message = "200"),
@ApiResponse(code = 500, message = "Internal Server Error") })
@GetMapping()
public ResponseEntity<Page<Employee>> getAllEmployeesPaginated(
@RequestParam(required = false, value="page") Integer page,
@RequestParam(required = false, value = "size") Integer size,
@RequestParam(required = false) String sortByParam,
@RequestParam(required = false) String direction,
Pageable pageable){
Page<Employee> employeePage = employeeService.findAllEmployees(pageable, page, size, sortByParam, direction);
return new ResponseEntity<>(employeePage, HttpStatus.OK);
}
}
AppPaginationConfig.java
@Configuration
public class AppPaginationConfig extends RepositoryRestMvcConfiguration{
public AppPaginationConfig(ApplicationContext context, ObjectFactory<ConversionService> conversionService) {
super(context, conversionService);
}
@Override
@Bean
public HateoasPageableHandlerMethodArgumentResolver pageableResolver() {
HateoasPageableHandlerMethodArgumentResolver resolver = super.pageableResolver();
resolver.setPageParameterName("page");
resolver.setSizeParameterName("size");
resolver.setOneIndexedParameters(true);
resolver.setFallbackPageable(PageRequest.of(0, 5));
return resolver;
}
}
当我不填充任何值时(请参见此处的屏幕截图:Unable to override defaults of Pagination in Spring Boot 2.1.5.RELEASE?),默认情况下为EmployeeController
值,但是当我填充"size", "page"
变量时,然后{ {1}}对象创建了我要发送的详细信息。看起来Pageable
和"pageNumber"
与"pageSize"
和"size"
无关。
Request1:"page"
注意:它将覆盖默认值 页面请求[数字:0,大小4,类别:未排序]
Request2:http://localhost:8080/employees?page=0&size=4
注意:我看到可分页对象的默认值是 页面请求[number:0,size 5,sort:UNSORTED] ,它不会覆盖默认值。