0

Hello I would like to write a Get method with Spring Rest but my code is not working code is below;

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

Thank you in advance

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
phantasma
  • 27
  • 5

2 Answers2

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

You are using @PathVariable but there is no param to your url mapping. You can fix like

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

You can change order of url mapping params. So if you dont wants in url mapping, you can get params with @RequestParam tags.

@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);
}
drowny
  • 2,067
  • 11
  • 19
0

You have to receive path variables in URL so for doing that add all params like below.

Change

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

to

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

I will recommend you to use @RequestParam for this.

See this : @RequestParam vs @PathVariable

Alien
  • 15,141
  • 6
  • 37
  • 57