简体   繁体   English

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

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

We are trying to do pagination with the help of this我们正在尝试借助此功能进行分页

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

With request parameters its fine, but when we try with Path variables its giving exception saying使用请求参数很好,但是当我们尝试使用Path variables时,它给出异常说

Requested path variable {destUserId} is not present in Controller request mapping annotations Controller 请求映射注释中不存在请求的路径变量 {destUserId}

The following are the methods we tried以下是我们尝试过的方法

Method-1: With @PathVariable方法一:使用@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;
}

Method-2: Without @PathVariable方法 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;
}

Method-3: RequestMapping with just path and with @PathVariable方法 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;
}

Method-4: RequestMapping with just path and without @PathVariable方法 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 with just path, without @PathVariable, without @Conjunction方法 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;
}

Method-6: GetMapping without @PathVariable, without @Conjunction方法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;
}

References参考

  1. Path variable support路径变量支持
  2. @PathVariable issue @PathVariable 问题

The problem is we can access the PathVariable as a method argument, but when we try to specify it in that in pathVars in the above cases execution is not reaching our Controller and we are getting the same above exception.问题是我们可以将PathVariable作为方法参数访问,但是当我们尝试在上述情况下在pathVars中指定它时,执行没有到达我们的 Controller 并且我们得到了相同的上述异常。 Any help?有什么帮助吗?

One thing I noticed you used GetMapping only in the last example.我注意到您只在最后一个示例中使用了 GetMapping 的一件事。 Is there reason for using RequestMapping?有使用 RequestMapping 的理由吗?

Is there a specific reason to not use [JPA pagination][https://www.baeldung.com/jpa-pagination]?是否有不使用 [JPA 分页][https://www.baeldung.com/jpa-pagination] 的特定原因?

Have you tried the following?您是否尝试过以下操作?

@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.

相关问题 这个 Spring Boot controller 方法注释有什么问题? (请求的路径在 class 级别定义,路径变量在方法中定义) - What is wrong in this Spring Boot controller method annotation? (the path of the request is defined at class level and the path variable at method) 在 Spring 的 GET 请求中将请求参数/路径变量映射到 Dto - Mapping request params/ path variable to Dto in GET request in Spring 路径变量未映射Spring MVC - Path Variable not mapping Spring MVC 具有路径变量的Spring MVC映射 - Spring MVC mapping with path variable 带有路径变量的 Spring Boot AuthenticationToken - Spring Boot AuthenticationToken with path variable Spring 引导 Controller 不将路径变量识别为可选 - Spring Boot Controller does not recognize Path Variable as optional Spring请求映射到特定路径变量值的不同方法 - Spring request mapping to a different method for a particular path variable value 使用Spring将路径变量传递给控制器 - Passing A Path Variable To A Controller With Spring 为什么只有当控制器请求映射注释中存在一个级别的URL路径时,Spring才会解析视图 - Why does spring resolve views only when one level url path present on controller request mapping annotation 用于根路径的Spring Boot请求映射控制器仅适用于双斜杠 - Spring Boot request mapping Controller for root path working only with double slash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM