繁体   English   中英

Spring 中的分页通过路径变量引导 - Controller 请求映射注释中不存在请求的路径变量

[英]Pagination in Spring Boot via path variable - Requested path variable is not present in Controller request mapping annotations

我们正在尝试借助此功能进行分页

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

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

Controller 请求映射注释中不存在请求的路径变量 {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", params = {"meta_type"}, spec = Equal.class))},
            and = @Spec(path = "destUserId", pathVars = "destUserId", spec = Equal.class)) Specification<UserData> spec) {
        
        
    return null;
}

方法 3:仅使用路径和 @PathVariable 的 RequestMapping

@RequestMapping("/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;
}

方法 4:仅使用路径而不使用 @PathVariable 的 RequestMapping

@RequestMapping("/cf/{destUserId}")
@ResponseBody
public PagedResponse<SystemStock> croudFundReport(@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;
}

方法 5:仅使用路径的 RequestMapping,不带 @PathVariable,不带 @Conjunction

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

方法6:没有@PathVariable,没有@Conjunction 的GetMapping

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

参考

  1. 路径变量支持
  2. @PathVariable 问题

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

我注意到您只在最后一个示例中使用了 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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM