Java 我的PathVariables不工作
您好,我想用SpringREST编写一个Get方法,但我的代码不起作用,下面是代码Java 我的PathVariables不工作,java,spring,rest,spring-boot,Java,Spring,Rest,Spring Boot,您好,我想用SpringREST编写一个Get方法,但我的代码不起作用,下面是代码 @RequestMapping(value = "userRight/hasRightForOperation", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<?&g
@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\u JSON\u value,products=MediaType.APPLICATION\u JSON\u value)
public ResponseEntity hasRightForOperation(@PathVariable(value=“loginName”)字符串loginName,
@PathVariable(value=“vendorId”)字符串vendorId,
@PathVariable(value=“accessRightCode”)字符串accessRightCode){
返回新的ResponseEntity(hasRightForOperation(loginName、vendorId、accessRightCode)、HttpStatus.OK);
提前感谢您您必须在URL中接收路径变量,以便添加以下所有参数 改变
@RequestMapping(value = "userRight/hasRightForOperation", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
到
我建议您为此使用@RequestParam
请参见:@RequestMapping(value=“userRight/hasRightForOperation”,method=RequestMethod.GET,consumes=MediaType.APPLICATION\u JSON\u value,products=MediaType.APPLICATION\u JSON\u value)
public ResponseEntity hasRightForOperation(@PathVariable(value=“loginName”)字符串loginName,
@PathVariable(value=“vendorId”)字符串vendorId,
@PathVariable(value=“accessRightCode”)字符串accessRightCode){
返回新的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);
@RequestMapping(value=“userRight/hasRightForOperation/{loginName}/{vendorId}/{accessRightCode}”,method=RequestMethod.GET,consumes=MediaType.APPLICATION\u JSON\u value,products=MediaType.APPLICATION\u JSON\u value)
public ResponseEntity hasRightForOperation(@PathVariable(value=“loginName”)字符串loginName,
@PathVariable(value=“vendorId”)字符串vendorId,
@PathVariable(value=“accessRightCode”)字符串accessRightCode){
返回新的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);
}
@GetMapping(value=“userRight/hasRightForOperation”,consumes=MediaType.APPLICATION\u JSON\u value,products=MediaType.APPLICATION\u JSON\u value)
public ResponseEntity hasRightForOperation(@RequestParam(“loginName”)字符串loginName,
@RequestParam(“vendorId”)字符串vendorId,
@RequestParam(“accessRightCode”)字符串(accessRightCode){
返回新的ResponseEntity(hasRightForOperation(loginName、vendorId、accessRightCode)、HttpStatus.OK);
}
URL映射中的路径变量在哪里?我想你必须使用@RequestParam,我想你应该使用一个参数。只需删除@PathVariable(…)
annotation。否则,请显示您发送GET请求的URL。谢谢,但vendorId和AccessRightCode是否也需要根据该代码进行更改?是的,相应地更新了答案…确保在点击控制器时,参数应按相同的顺序。如果答案有帮助,请接受并投票我很高兴能帮助你
@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);
@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);
}