0

我有以下類:PagingAndSortingRepository分頁信息

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

當我打電話/書籍我得到很好的分頁信息,但findByTitle方法我沒有得到任何分頁信息(雖然分頁作品,沒有下一個,prev鏈接或任何「計數」字段)

我應該添加什麼以獲得與/ 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

我想你的版本庫應該返回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); 
} 
+0

現在距離它更近了一步,使用Page返回頁面統計信息(計數等),但HATEOAS鏈接(next,prev,last)仍然丟失。 :) – EralpB

+1

哦,它的工作,我一定錯過了我的第一次嘗試,謝謝! – EralpB