我的 PathVariables 不起作用

您好,我想用 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);

先感谢您


POPMUISE
浏览 250回答 2
2回答

侃侃尔雅

@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,&nbsp; &nbsp; &nbsp; &nbsp; @PathVariable(value = "vendorId") String vendorId,&nbsp; &nbsp; &nbsp; &nbsp; @PathVariable(value = "accessRightCode") String accessRightCode) {&nbsp; &nbsp; 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,&nbsp; &nbsp; &nbsp; &nbsp; @PathVariable(value = "vendorId") String vendorId,&nbsp; &nbsp; &nbsp; &nbsp; @PathVariable(value = "accessRightCode") String accessRightCode) {&nbsp; &nbsp; 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,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @RequestParam("vendorId") String vendorId,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @RequestParam("accessRightCode") String accessRightCode) {&nbsp; &nbsp; return new ResponseEntity<>(hasRightForOperation(loginName, vendorId, accessRightCode), HttpStatus.OK);}

回首忆惘然

您必须在 URL 中接收路径变量,以便添加如下所有参数。改变@RequestMapping(value&nbsp;=&nbsp;"userRight/hasRightForOperation",&nbsp;method&nbsp;=&nbsp;RequestMethod.GET,&nbsp;consumes&nbsp;=&nbsp;MediaType.APPLICATION_JSON_VALUE,&nbsp;produces&nbsp;=&nbsp;MediaType.APPLICATION_JSON_VALUE)到@RequestMapping(value&nbsp;=&nbsp;"userRight/hasRightForOperation/{loginName}/{vendorId}/{accessRightCode}",&nbsp;method&nbsp;=&nbsp;RequestMethod.GET,&nbsp;consumes&nbsp;=&nbsp;MediaType.APPLICATION_JSON_VALUE,&nbsp;produces&nbsp;=&nbsp;MediaType.APPLICATION_JSON_VALUE)我会推荐你使用@RequestParam这个。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java