【问题标题】:Spring Boot custom paging parameterSpring Boot 自定义分页参数
【发布时间】:2017-06-14 07:29:22
【问题描述】:

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

应用程序属性,但它似乎对项目没有任何影响。

我的控制器是这样的

@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 版本吗?

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


【解决方案1】:

此问题可能与 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

【讨论】:

  • 我正在使用父启动器 1.5.4.RELEASE,它仍然没有更新属性文件中的条目。我已经能够通过更改客户端参数来解决它,但我认为这会更容易。
猜你喜欢
  • 1970-01-01
  • 2016-10-31
  • 1970-01-01
  • 2011-05-17
  • 2020-04-26
  • 1970-01-01
  • 1970-01-01
  • 2020-04-12
  • 2014-11-30
相关资源
最近更新 更多