PagingAndSortingRepository分页信息

时间:2017-01-23 13:08:26

标签: spring spring-boot spring-data-rest spring-restcontroller spring-rest

我有以下课程:

@RepositoryRestResource(collectionResourceRel = "books", path = "books")
public interface BookRepository extends PagingAndSortingRepository<Book, Long> {
    List<Book> findByTitle(Pageable pageable, @Param("title") String title);
}

当我打电话/书籍时,我得到了很好的分页信息,但是使用findByTitle方法我没有得到任何分页信息(虽然分页有效,没有下一个,上一个链接,也没有任何&#34;数&#34;字段)

我应该添加什么才能获得与/ books响应相同的HATEOAS体验?

我打电话的时候     http://127.0.0.1:8888/books/search/findByTitle?title=bc&page=0

我收到了这个回复:

    {
  "_embedded" : {
    "books" : [ {
      "title" : "bc",
      "_links" : {
        "self" : {
          "href" : "http://127.0.0.1:8888/books/4"
        },
        "book" : {
          "href" : "http://127.0.0.1:8888/books/4"
        }
      }
    }, {
      "title" : "bc",
      "_links" : {
        "self" : {
          "href" : "http://127.0.0.1:8888/books/5"
        },
        "book" : {
          "href" : "http://127.0.0.1:8888/books/5"
        }
      }
    }, {
      "title" : "bc",
      "_links" : {
        "self" : {
          "href" : "http://127.0.0.1:8888/books/6"
        },
        "book" : {
          "href" : "http://127.0.0.1:8888/books/6"
        }
      }
    }, {
      "title" : "bc",
      "_links" : {
        "self" : {
          "href" : "http://127.0.0.1:8888/books/7"
        },
        "book" : {
          "href" : "http://127.0.0.1:8888/books/7"
        }
      }
    }, {
      "title" : "bc",
      "_links" : {
        "self" : {
          "href" : "http://127.0.0.1:8888/books/8"
        },
        "book" : {
          "href" : "http://127.0.0.1:8888/books/8"
        }
      }
    }, {
      "title" : "bc",
      "_links" : {
        "self" : {
          "href" : "http://127.0.0.1:8888/books/9"
        },
        "book" : {
          "href" : "http://127.0.0.1:8888/books/9"
        }
      }
    }, {
      "title" : "bc",
      "_links" : {
        "self" : {
          "href" : "http://127.0.0.1:8888/books/10"
        },
        "book" : {
          "href" : "http://127.0.0.1:8888/books/10"
        }
      }
    }, {
      "title" : "bc",
      "_links" : {
        "self" : {
          "href" : "http://127.0.0.1:8888/books/11"
        },
        "book" : {
          "href" : "http://127.0.0.1:8888/books/11"
        }
      }
    }, {
      "title" : "bc",
      "_links" : {
        "self" : {
          "href" : "http://127.0.0.1:8888/books/12"
        },
        "book" : {
          "href" : "http://127.0.0.1:8888/books/12"
        }
      }
    }, {
      "title" : "bc",
      "_links" : {
        "self" : {
          "href" : "http://127.0.0.1:8888/books/13"
        },
        "book" : {
          "href" : "http://127.0.0.1:8888/books/13"
        }
      }
    }, {
      "title" : "bc",
      "_links" : {
        "self" : {
          "href" : "http://127.0.0.1:8888/books/14"
        },
        "book" : {
          "href" : "http://127.0.0.1:8888/books/14"
        }
      }
    }, {
      "title" : "bc",
      "_links" : {
        "self" : {
          "href" : "http://127.0.0.1:8888/books/15"
        },
        "book" : {
          "href" : "http://127.0.0.1:8888/books/15"
        }
      }
    }, {
      "title" : "bc",
      "_links" : {
        "self" : {
          "href" : "http://127.0.0.1:8888/books/16"
        },
        "book" : {
          "href" : "http://127.0.0.1:8888/books/16"
        }
      }
    }, {
      "title" : "bc",
      "_links" : {
        "self" : {
          "href" : "http://127.0.0.1:8888/books/17"
        },
        "book" : {
          "href" : "http://127.0.0.1:8888/books/17"
        }
      }
    }, {
      "title" : "bc",
      "_links" : {
        "self" : {
          "href" : "http://127.0.0.1:8888/books/18"
        },
        "book" : {
          "href" : "http://127.0.0.1:8888/books/18"
        }
      }
    }, {
      "title" : "bc",
      "_links" : {
        "self" : {
          "href" : "http://127.0.0.1:8888/books/19"
        },
        "book" : {
          "href" : "http://127.0.0.1:8888/books/19"
        }
      }
    }, {
      "title" : "bc",
      "_links" : {
        "self" : {
          "href" : "http://127.0.0.1:8888/books/20"
        },
        "book" : {
          "href" : "http://127.0.0.1:8888/books/20"
        }
      }
    }, {
      "title" : "bc",
      "_links" : {
        "self" : {
          "href" : "http://127.0.0.1:8888/books/21"
        },
        "book" : {
          "href" : "http://127.0.0.1:8888/books/21"
        }
      }
    }, {
      "title" : "bc",
      "_links" : {
        "self" : {
          "href" : "http://127.0.0.1:8888/books/22"
        },
        "book" : {
          "href" : "http://127.0.0.1:8888/books/22"
        }
      }
    }, {
      "title" : "bc",
      "_links" : {
        "self" : {
          "href" : "http://127.0.0.1:8888/books/23"
        },
        "book" : {
          "href" : "http://127.0.0.1:8888/books/23"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://127.0.0.1:8888/books/search/findByTitle?title=bc&page=0"
    }
  }
}

1 个答案:

答案 0 :(得分:1)

我认为您的存储库应该返回org.springframework.data.domain.Page而不是List

@RepositoryRestResource(collectionResourceRel = "books", path = "books")
public interface BookRepository extends PagingAndSortingRepository<Book, Long> {
    Page<Book> findByTitle(Pageable pageable, @Param("title") String title);
}