java - Spring Boot自定义分页参数

标签 java spring hibernate spring-mvc spring-data-jpa

您好,我正在尝试覆盖 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/

相关文章:

java - Tomcat 上的 Alfresco Community 启动非常慢

Spring 批处理 : Which ItemReader implementation to use for high volume & low latency

java - Ehcache 与静态 map 缓存实现

Oracle selectsequence.nextval from Dual 听起来太慢

java - 与Spring Cloud Sleuth一起使用时,Spring Integration错误 channel 处理中断

java - 由于 'org.hibernate.HibernateException: No Session found for current thread' 异常,需要 @Transactional 注释

java - HashSet 插入 2 个相等的元素

java - 扩展 java swing 按钮?

java - Spring Boot生成的WAR文件,是标准的ZIP格式吗?

java - Hazelcast Spring 配置