elasticsearch findAll with pageable

时间:2018-04-11 18:31:09

标签: elasticsearch spring-data-elasticsearch

我需要使用pageable获取所有文档,但是我收到此错误

  

com.fasterxml.jackson.databind.JsonMappingException :(是   java.lang.NullPointerException)(通过引用链:   org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl [ “刻面”])

存储库

public interface ConventionSearchRepository extends ElasticsearchRepository<Convention, Long> {
}

服务

@Service
@Transactional
public class ConventionServiceImpl implements ConventionService {

  private final ConventionSearchRepository conventionSearchRepository;

   @Override
   @Transactional(readOnly = true)
   public Page<Convention> findAll(final PageRequest pageable) {
     return conventionSearchRepository.findAll(pageable);
   }

}

控制器

  @GetMapping("/conventions")
  public Page<Convention> getAllConventions(@RequestParam(value = "_page", required = false, defaultValue = "0") int page,
      @RequestParam(value = "_perPage", required = false, defaultValue = "20") int perPage,
      @RequestParam(value = "_sortDir", required = false, defaultValue = "DESC") Sort.Direction sortDir,
      @RequestParam(value = "_sortBy", required = false, defaultValue = "shortname") String sortBy) {
    Sort sort = new Sort(sortDir, sortBy);
    PageRequest pageRequest = new PageRequest(page, perPage, sort);
    return conventionService.findAll(pageRequest);
  }

1 个答案:

答案 0 :(得分:0)

我能够简单地向我的@Controller声明一个org.springframework.data.domain.Pageable参数,然后将其直接传递给我的服务,然后最终传递给@Repository(我用CustomRepository扩展它)。 Spring负责填充有关排序参数等的所有细节。我没有必要详细定义单独的@RequestParams。

这是一个例子

public Page<LogEntry> findAllLogEntries(@PageableDefault(page = DEFAULT_PAGE, size = DEFAULT_PAGE_SIZE) @SortDefault.SortDefaults({@SortDefault(sort = "@timestamp", direction = Direction.DESC)}) Pageable pageable) {

我在Spring 5和Spring Data Elasticsearch 3.0.5.RELEASE