Reputation: 385
I saw some questions about this error but I could not find a solution for my case.
I'm implementing a paging in the spring boot application.
I Have this method in my controller
@RequestMapping(method = RequestMethod.GET, value = "/distrito", params = { "page", "size" })
public ResponseEntity<Page<Distritos>> buscarTodosDistritos(HttpServletRequest request, @RequestParam("page") int page, @RequestParam("size") int size) throws ServletException {
Map<String, String>informacaoUsuario = uService.getInformacoesUsuario(request);
Page<Distritos> distritosBuscados = distritosService.buscarFiltro(Long.parseLong(informacaoUsuario.get("idEntidadeSelecionada")), page, size);
return new ResponseEntity<>(distritosBuscados, HttpStatus.OK);
}
and my service
public Page<Distritos> buscarFiltro(Long usuarioEntidade ,int size, int page){
return distritosRepository.encontrar(usuarioEntidade, size, page);
}
my repository
@Query( nativeQuery=true, value="SELECT dist.nome, dist.id_distrito, dist.id_entidade, dist.id_municipio, dist.id_uf, dist.codigo_dne, dist.flag_ativo, enti.nome Entidade, muni.nome Municipio, unfe.nome UF FROM glb.distritos dist, glb.entidades enti, glb.municipios muni, glb.ufs unfe WHERE dist.id_entidade = enti.id_entidade AND dist.id_municipio = muni.id_municipio AND muni.id_uf = unfe.id_uf and enti.id_entidade = :parametroId order by nome ")
public Page<Distritos> encontrar(@Param ("parametroId")Long usuarioEntidade, int size, int page);
and i got this error
Caused by: java.lang.IllegalArgumentException: Either use @Param on all parameters except Pageable and Sort typed once, or none at all!
at org.springframework.util.Assert.isTrue(Assert.java:92) ~[spring-core-4.3.9.RELEASE.jar:4.3.9.RELEASE]
at org.springframework.data.repository.query.Parameters.assertEitherAllParamAnnotatedOrNone(Parameters.java:297) ~[spring-data-commons-1.13.4.RELEASE.jar:na]
at org.springframework.data.repository.query.Parameters.<init>(Parameters.java:91) ~[spring-data-commons-1.13.4.RELEASE.jar:na]
how can i solve that ???
Upvotes: 2
Views: 5401
Reputation: 59978
You have to pass a Pageable
Object and not the size
and page
like you do :
public Page<Distritos> encontrar(@Param ("parametroId") Long usuarioEntidade,
Pageable pageable);
and call your method like this
return distritosRepository.encontrar(usuarioEntidade, new PageRequest(size, page));
You can either create a Pageable Object which hold a sorted attribute, so instead of using order by nome
in your query you can use :
Sort sort = new Sort(new Sort.Order(Direction.ASC, "nome"));
Pageable pageable = new PageRequest(size, page, sort);
return distritosRepository.encontrar(usuarioEntidade, pageable);
Upvotes: 2
Reputation: 26522
Pass the Pageable
object instead:
public Page<Distritos> buscarFiltro(Long usuarioEntidade ,int size, int page){
Pageable pageable = new PageRequest(page, size);
return distritosRepository.encontrar(usuarioEntidade, pageable);
}
and repo:
public Page<Distritos> encontrar(@Param ("parametroId")Long usuarioEntidade
, Pageable pageable);
Upvotes: 0
Reputation: 1153
Check org.springframework.data.domain.Pageable
class which is provided by Spring for pagination. Controller will extract your parameters and build it automatically.
Upvotes: 1