コレクションの内容をリストする単純な REST API を取得しようとしてきましたが、マトリックス変数を使用してページネーションを制御しています。
私のコントローラーには、コレクションの内容を一覧表示する次のメソッドがあります。
@RequestMapping(
value = "articles",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ArticlePageRestApiResponse listArticles(
@MatrixVariable(required = true, defaultValue = 100, value = "resultsPerPage") int resultsPerPage,
@MatrixVariable(required = true, defaultValue = 0, value = "pageNumber") int pageNumber) {
// some logic to return the collection
}
次に GET http://example.com/articles;resultsPerPage=22;pageNumber=33を実行すると、リクエスト マッピングが見つかりません。以下を追加して、行列変数のサポートを有効にしました。
@Configuration
public class EnableUriMatrixVariableSupport extends WebMvcConfigurationSupport {
@Override
@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
RequestMappingHandlerMapping hm = super.requestMappingHandlerMapping();
hm.setRemoveSemicolonContent(false);
return hm;
}
}
私が見つけたのは、マトリックス変数の前に少なくとも 1 つのテンプレート変数が付けられている場合、マトリックス変数が正しく割り当てられていることです。以下は機能しますが、URI パスの一部を常に「記事」にするテンプレート変数にして、リクエスト マッピング ハンドラーをだまして少なくとも 1 つの URI テンプレート変数があると思わせる必要があるところが醜いです。
@RequestMapping(
value = "{articles}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ArticlePageRestApiResponse listArticles(
@PathVariable("articles") String ignore,
@MatrixVariable(required = true, defaultValue = 100, value = "resultsPerPage") int resultsPerPage,
@MatrixVariable(required = true, defaultValue = 0, value = "pageNumber") int pageNumber) {
// some logic to return the collection
}
バグを見つけましたか、それともマトリックス変数を誤解していますか?