为了账号安全,请及时绑定邮箱和手机立即绑定

我的 PathVariables 不起作用

我的 PathVariables 不起作用

POPMUISE 2021-08-19 16:44:40
您好,我想用 Spring Rest 编写一个 Get 方法,但我的代码不起作用,代码如下;@RequestMapping(value = "userRight/hasRightForOperation", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)    public ResponseEntity<?> hasRightForOperation(@PathVariable(value = "loginName") String loginName,            @PathVariable(value = "vendorId") String vendorId,            @PathVariable(value = "accessRightCode") String accessRightCode) {        return new ResponseEntity<>(hasRightForOperation(loginName, vendorId, accessRightCode), HttpStatus.OK);先感谢您
查看完整描述

2 回答

?
侃侃尔雅

TA贡献1801条经验 获得超15个赞

@RequestMapping(value = "userRight/hasRightForOperation", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)

public ResponseEntity<?> hasRightForOperation(@PathVariable(value = "loginName") String loginName,

        @PathVariable(value = "vendorId") String vendorId,

        @PathVariable(value = "accessRightCode") String accessRightCode) {

    return new ResponseEntity<>(hasRightForOperation(loginName, vendorId, accessRightCode), HttpStatus.OK);

您正在使用 @PathVariable 但您的 url 映射没有参数。你可以修复像


@RequestMapping(value = "userRight/hasRightForOperation/{loginName}/{vendorId}/{accessRightCode}", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)

public ResponseEntity<?> hasRightForOperation(@PathVariable(value = "loginName") String loginName,

        @PathVariable(value = "vendorId") String vendorId,

        @PathVariable(value = "accessRightCode") String accessRightCode) {

    return new ResponseEntity<>(hasRightForOperation(loginName, vendorId, accessRightCode), HttpStatus.OK);

您可以更改 url 映射参数的顺序。因此,如果您不想使用 url 映射,则可以使用 @RequestParam 标签获取参数。


@GetMapping(value = "userRight/hasRightForOperation", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)

public ResponseEntity<?> hasRightForOperation(@RequestParam("loginName") String loginName,

                                              @RequestParam("vendorId") String vendorId,

                                              @RequestParam("accessRightCode") String accessRightCode) {

    return new ResponseEntity<>(hasRightForOperation(loginName, vendorId, accessRightCode), HttpStatus.OK);

}


查看完整回答
反对 回复 2021-08-19
?
回首忆惘然

TA贡献1847条经验 获得超11个赞

您必须在 URL 中接收路径变量,以便添加如下所有参数。

改变

@RequestMapping(value = "userRight/hasRightForOperation", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)

@RequestMapping(value = "userRight/hasRightForOperation/{loginName}/{vendorId}/{accessRightCode}", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)

我会推荐你使用@RequestParam这个。


查看完整回答
反对 回复 2021-08-19
  • 2 回答
  • 0 关注
  • 218 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信