I am trying to get a simple REST API to display the contents of a collection, and I am using matrix variables to control pagination.
My controller has the following method for listing the contents of a collection:
@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) {
If I then do a GET http://example.com/articles;resultsPerPage=22;pageNumber=33 , it will not be able to find the display of the request. I have included support for matrix variables by adding the following:
@Configuration public class EnableUriMatrixVariableSupport extends WebMvcConfigurationSupport { @Override @Bean public RequestMappingHandlerMapping requestMappingHandlerMapping() { RequestMappingHandlerMapping hm = super.requestMappingHandlerMapping(); hm.setRemoveSemicolonContent(false); return hm; } }
I found that if matrix variables have a prefix with at least one template variable, then matrix variables are correctly assigned. The following works, but ugly, where I had to make part of the URI path a template variable, which would always be βarticlesβ, to trick the query matching handler into thinking that there was at least one URI template variable:
@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) {
I found an error or am I misunderstanding the matrix variables?