【问题标题】:Pagination in Spring Boot via path variable - Requested path variable is not present in Controller request mapping annotations通过路径变量在 Spring Boot 中进行分页 - 控制器请求映射注释中不存在请求的路径变量
【发布时间】:2021-02-23 12:33:45
【问题描述】:

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

<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", 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;
}

Method-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:GetMapping 不带@PathVariable,不带@Conjunction

@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. Path variable support
  2. @PathVariable issue

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

【问题讨论】:

    标签: java spring-boot pagination request-mapping path-variables


    【解决方案1】:

    有一件事我注意到你只在最后一个例子中使用了 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
    }
    

    【讨论】:

    • 这是一个使用路径变量的基本 GET API。我正在寻找使用 @Spec 和 @Conjunction 等注释的分页,您共享的代码将适用于我。问题是当我在规范中指定路径变量时
    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 2012-04-21
    • 2019-05-12
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多