除了Pageable之外的所有参数都使用@Param,并在Spring引导后键入Sort

时间:2017-12-08 10:55:21

标签: java spring spring-boot pagination spring-data

我看到了一些关于此错误的问题,但我无法找到解决方案。

我在spring boot应用程序中实现了分页。

我在我的控制器中有这个方法

@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);
    } 

和我的服务

    public Page<Distritos> buscarFiltro(Long usuarioEntidade ,int size, int page){
        return distritosRepository.encontrar(usuarioEntidade, size, page);
    }

我的资料库

@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);

我收到了这个错误

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]

我该如何解决?

3 个答案:

答案 0 :(得分:1)

你必须传递一个Pageable对象,而不是sizepage

public  Page<Distritos>  encontrar(@Param ("parametroId") Long usuarioEntidade, 
                                                          Pageable pageable);

并像这样调用你的方法

return distritosRepository.encontrar(usuarioEntidade, new PageRequest(size, page));

您可以创建一个包含已排序属性的Pageable对象,因此您可以在查询中使用order by nome而不是Sort sort = new Sort(new Sort.Order(Direction.ASC, "nome")); Pageable pageable = new PageRequest(size, page, sort); return distritosRepository.encontrar(usuarioEntidade, pageable);

public static void longClickView(View v) {

    final int viewWidth = v.getWidth();
    final int viewHeight = v.getHeight();

    final float x = viewWidth / 2.0f;
    final float y = viewHeight / 2.0f;

    long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis();

    MotionEvent event = MotionEvent.obtain(downTime, eventTime,
            MotionEvent.ACTION_DOWN, x, y, 0);
    v.onTouchEvent(event);

    eventTime = SystemClock.uptimeMillis();
    final int touchSlop = ViewConfiguration.get(v.getContext()).getScaledTouchSlop();
    event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE,
            x + touchSlop / 2, y + touchSlop / 2, 0);
    v.onTouchEvent(event);

    v.postDelayed(() -> {
        long eventTime2 = SystemClock.uptimeMillis();
        MotionEvent event2 = MotionEvent.obtain(downTime, eventTime2, MotionEvent.ACTION_UP, x, y, 0);
        v.onTouchEvent(event2);
    }, (long) (ViewConfiguration.getLongPressTimeout() * 1.5f));
}

答案 1 :(得分:0)

检查Spring提供的org.springframework.data.domain.Pageable类的分页。 Controller将提取您的参数并自动构建它。

答案 2 :(得分:0)

改为传递Pageable对象:

public Page<Distritos> buscarFiltro(Long usuarioEntidade ,int size, int page){
       Pageable pageable = new PageRequest(page, size);
       return distritosRepository.encontrar(usuarioEntidade, pageable);
}

和回购:

public  Page<Distritos>  encontrar(@Param ("parametroId")Long usuarioEntidade
         , Pageable pageable);