Kacper
Kacper

Reputation: 1090

@PathVariable and @RequestParam in one query

I have a request which doesn't work

http://localhost:8070/promo/lock/1?reason=CONSUMED

There is my config of an endpoint

@RequestMapping(name = SERVICE_URL + "/{id}", method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.NO_CONTENT)
public void unlock(Authentication authentication, @RequestParam(value = "reason") UnlockReason reason, @PathVariable Long id)

I get the exception

rg.springframework.web.bind.ServletRequestBindingException: Missing URI template variable 'id' for method parameter of type Long

What is wrong with that?

Upvotes: 1

Views: 3789

Answers (2)

Mitali Jain
Mitali Jain

Reputation: 49

If the name of the method argument matches the name of the path variable exactly, then it can be simplified by using @PathVariable with no value: So this is correct :

public void unlock(Authentication authentication, @RequestParam(value = "reason") UnlockReason reason, @PathVariable Long id) 

If you want to give it some other name, you need to do this by :

public void unlock(Authentication authentication, @RequestParam(value = "reason") UnlockReason reason, @PathVariable("id") Long number)

Upvotes: 0

Karthik R
Karthik R

Reputation: 5786

There are two issues is your annotation mappings:

  • @RequestMapping : It should take the URI path as 'path' and not as name.

    @RequestMapping(path = SERVICE_URL + "/{id}", method = RequestMethod.DELETE)
    
  • @PathVariable : Should have the path param name, as the default value is "".

    public void unlock(Authentication authentication, @RequestParam(value = "reason") UnlockReason reason, @PathVariable("id") Long id
    

Upvotes: 1

Related Questions