![](/img/trans.png)
[英]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)
[英]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;
}
參考
問題是我們可以將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.