gpt4 book ai didi

java - 我的 PathVariables 不起作用

转载 作者:行者123 更新时间:2023-12-02 10:52:17 25 4
gpt4 key购买 nike

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

提前谢谢

最佳答案

@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,但您的网址映射没有参数。您可以像这样修复

@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);
}

关于java - 我的 PathVariables 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52071351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com