通过路径变量在 Spring Boot 中进行分页 - 控制器请求映射注释中不存在请求的路径变量

问题描述

我们正在尝试在此帮助下进行分页

<dependency>
    <groupId>net.kaczmarzyk</groupId>
    <artifactId>specification-arg-resolver</artifactId>
    <version>2.1.1</version>
</dependency>

使用请求参数很好,但是当我们尝试使用 Path variables 时它给出异常说

请求的路径变量 {destUserId} 不存在于控制器请求映射注释中

以下是我们尝试过的方法

方法一:使用@PathVariable

@RequestMapping(method = RequestMethod.GET,value = "/cf/{destUserId}")
@ResponseBody
public PagedResponse<SystemStock> croudFundReport(@PathVariable(name = "destUserId") String eventId,@Conjunction (value = {
            @Or(value = @Spec(path = "MetaType",params = {"Meta_type"},spec = Equal.class))},and = @Spec(path = "destUserId",pathVars = "destUserId",spec = Equal.class)) Specification<UserData> spec) {
        
        
    return null;
}

方法 2:没有@PathVariable

@RequestMapping(method = RequestMethod.GET,value = "/cf/{destUserId}")
@ResponseBody
public PagedResponse<SystemStock> croudFundReport(@Conjunction (value = {
            @Or(value = @Spec(path = "MetaType",spec = Equal.class)) Specification<UserData> spec) {
        
        
    return null;
}

方法 3 : RequestMapping 只带路径和@PathVariable

@RequestMapping("/cf/{destUserId}")
@ResponseBody
public PagedResponse<SystemStock> croudFundReport(@PathVariable(name = "destUserId") String eventId,spec = Equal.class)) Specification<UserData> spec) {
        
        
    return null;
}

方法 4:RequestMapping 只带路径,不带 @PathVariable

@RequestMapping("/cf/{destUserId}")
@ResponseBody
public PagedResponse<SystemStock> croudFundReport(@Conjunction (value = {
            @Or(value = @Spec(path = "MetaType",spec = Equal.class)) Specification<UserData> spec) {
        
        
    return null;
}

Method-5 : RequestMapping 只带路径,不带@PathVariable,不带@Conjunction

@RequestMapping("/cf/{destUserId}")
@ResponseBody
public PagedResponse<SystemStock> croudFundReport(@Spec(path = "destUserId",spec = Equal.class) Specification<UserData> spec) {
        
    
    return null;
}

Method-6 : GetMapping 不带@PathVariable,不带@Conjunction

@GetMapping(path = "/cf/{destUserId}",produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public PagedResponse<SystemStock> croudFundReport(@Spec(path = "destUserId",spec = Equal.class) Specification<UserData> spec) {
    
    
    return null;
}

参考资料

  1. Path variable support
  2. @PathVariable issue

问题是我们可以访问 PathVariable 作为方法参数,但是当我们尝试在 pathVars 中指定它时,在上述情况下,执行没有到达我们的控制器,我们得到了同上例外。有什么帮助吗?

解决方法

我注意到您仅在上一个示例中使用了 GetMapping 的一件事。有理由使用RequestMapping吗?

不使用 [JPA 分页][https://www.baeldung.com/jpa-pagination] 是否有特定原因?

您是否尝试过以下操作?

@GetMapping(path = "/cf/{destUserId}",produces = MediaType.APPLICATION_JSON_VALUE)
public PagedResponse<SystemStock> croudFundReport( @PathVariable(name = "destUserId") final String destUserId) {
 // TODO implementation
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...