您好,我正在尝试覆盖 Spring JPA 中页面大小的默认参数名称,以匹配需要使用的 Kendo UI 网格的参数名称
http://localhost:8080/retailers/all?page=1&pageSize=5
JPA 正在制作
http://localhost:8080/retailers/all?page=1&size=5
我尝试添加
spring.data.rest.page-param-name=page
spring.data.rest.limitParamName=pageSize
应用程序属性,但它似乎对项目没有任何影响。
我的 Controller 看起来像这样
@RequestMapping(method = RequestMethod.GET, value = "retailers/all")
public ResponseEntity<Page<RetailerEntity>> retailers(Pageable pageable){
Page<RetailerEntity> retailers = retailerService.getAllRetailers(pageable);
return new ResponseEntity<>(retailers, HttpStatus.OK);
}
并且存储库正在使用开箱即用的实现
public interface RetailerRepository extends PagingAndSortingRepository<RetailerEntity, Integer> {
}
感谢任何帮助。
最佳答案
此问题可能与 spring boot 版本有关。 更改 application.properties 仅适用于 Spring Boot 1.2+。 如果您使用的是 1.1 或更早版本,您有两个选择:
1) 使用 RepositoryRestConfigurerAdapter
的自定义实现创建一个 RepositoryRestConfigurer
bean。
@Configuration
class CustomRestMvcConfiguration {
@Bean
public RepositoryRestConfigurer repositoryRestConfigurer() {
return new RepositoryRestConfigurerAdapter() {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.setBasePath("/api");
}
};
}
}
2) 使用 RepositoryRestConfigurer
的自定义实现创建一个组件。
@Component
public class CustomizedRestMvcConfiguration extends RepositoryRestConfigurerAdapter {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.setBasePath("/api");
}
}
这些示例适用于 basePath
属性,您可以以相同的方式更改所有其他属性。
您可以查看更多详情:the documentation
关于java - Spring Boot自定义分页参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44538241/