在@PathVariable和@RequestParam中使参数可选

问题描述 投票:0回答:3

我正在制作一个控制器,它接受两个参数作为@PathVariable,并且我想让第二个参数可选,所以我这样做了->

@GetMapping(value = {"${URL.COACH.GET.ALL.COACH.BY.CORPORATE.NAME}"})
public ResponseEntity<List<CoachRelatedDetails>> getCoachDetailsByCorporateName(@PathVariable String corporateName , @PathVariable(required = false) RoleType roleType){
    System.out.println("ins controller");
    return coachFacade.getCoachDetails(corporateName,roleType);
}

哪里->

URL.COACH.GET.ALL.COACH.BY.CORPORATE.NAME=/getAllCoachByCorporateName/{corporateName}/{roleType}

我在第二个参数中添加了

required = false
。 但是当像这样调用它时,它会给出错误:找不到路径
http://localhost:8080/getAllCoachByCorporateName/ABC/

在使用 @RequestParam 做同样的事情时,我得到了同样的错误

@GetMapping(value = {"${URL.COACH.GET.ALL.COACH.BY.CORPORATE.NAME}"})
public ResponseEntity<List<CoachRelatedDetails>> getCoachDetailsByCorporateName
                 (@RequestParam String corporateName,
                  @RequestParam(required = false ) RoleType roleType) {
    System.out.println("ins controller");
    return coachFacade.getCoachDetails(corporateName, roleType);
}

http://localhost:8080/getAllCoachByCorporateName?corporateName=ABC

我在网上找到了相关文章,他们也是这样做的。我不知道我哪里错了。

java spring spring-boot
3个回答
1
投票

我什至不知道 @PathVariable 有一个必需的属性,这不是最佳实践,如果您有一个可选参数,您应该将其分配为请求参数而不是路径变量。所以你的路径看起来像这样

URL.COACH.GET.ALL.COACH.BY.CORPORATE.NAME=/getAllCoachByCorporateName

你的 get 方法会是这样的

@GetMapping(value = {"${URL.COACH.GET.ALL.COACH.BY.CORPORATE.NAME}"})
public ResponseEntity<List<CoachRelatedDetails>> getCoachDetailsByCorporateName
                 (@RequestParam(required = false ) String corporateName,
                  @RequestParam(required = false ) RoleType roleType) {
    System.out.println("ins controller");
    return coachFacade.getCoachDetails(corporateName, roleType);
}

然后你的http请求将是

http://localhost:8080/getAllCoachByCorporateName?corporateName=ABC

这应该可以解决您的问题,我还建议该路径基于资源,因此您可以将其更改为 /coaches,然后传递 PathVariables 和 RequestParams 来获取您想要的任何内容


1
投票

问题

你在 PathVariable 的使用方式上有错误,我不明白为什么使用 SpringEL 来定义 Controller URI,在大多数流行的场景中,这种方式是不必要的。
回到主题,您可能想知道为什么代码中的

@PathVariable
用法会遇到错误。

解决方案

只需删除

@PathVariable(required = false) RoleType roleType
,如果您尝试使用
@PathVariable
接收复杂的数据结构,可能会出现一些潜在的问题,例如
Serialization/Deserialization
URL Encoding

作为另一项建议,使用

@RequestBody
接收复杂的数据结构。

同样的原理

@RequestParm
,不适合
URL.COACH.GET.ALL.COACH.BY.CORPORATE.NAME=/getAllCoachByCorporateName/{corporateName}/{roleType}
这样的URI,如果你想用这种方式请求,只需删除tail中多余的uri,然后重新发出请求即可。

附录

我认为阅读SpringDoc可能对你加深知识更有帮助


1
投票

我假设 RoleType 是一种枚举类型,如果是的话,您需要在 Pathvariable 的情况下创建一个处理程序端点,如下所示:

    @GetMapping(value = {"/testing/{corporateName}"})
    public ResponseEntity<List<CoachRelatedDetails>>getCoachDetailsByCorporateNameOnly(@PathVariable String corporateName) {
        System.out.println("when role type is optional call getCoachDetailsByCorporateNameOnly() ");
        return coachFacade.getCoachDetails(corporateName);
    }

对于RequestParam,您可以参考我对此的回答

© www.soinside.com 2019 - 2024. All rights reserved.