2

I have following method in controller:

 @RequestMapping(value = "/items/{identifier}", method = RequestMethod.DELETE)
    public void delete(@PathVariable String identifier) {
    ... 
    // find item in database by identifier
}

The problem is identifier is a String that can contain slash ('/'). I.e. situation can occurs:

http://myhost/items/file:/123

For given URL 'identifier' must be "file:/123".

And if there is a '/', 405 (method is not allowed) throws due to incorrect URL.

How to tell Spring that it should take all after '/items/' as 'identifier' (resource name in REST terms)?

EDIT: I was told that it is client's responsibility to manage slashes and encoding, so I left it as it was.

But for the info, based on ankur-singhal answer in comments:

Support that I wanted to was added in Spring 4.1.0, see this link and this commit. So in my case it'll be:

 @RequestMapping(value = "/items/{/identifier}", method = RequestMethod.DELETE)

'/' before 'identifier' did a trick.

Community
  • 1
  • 1
fasth
  • 2,232
  • 6
  • 26
  • 37

1 Answers1

1

Try changing code to below, see if it works

It will accept any value for identifier.

 @RequestMapping(value = "/items/{identifier:.*}", method = RequestMethod.DELETE)
    public void delete(@PathVariable String identifier) {
    ... 
    // find item in database by identifier
}

If you use identifier as request parameter then you can achieve it as follows :

 @RequestMapping(value = "/items", method = RequestMethod.DELETE)
    public void delete(@RequestParam("identifier") String identifier) {
    ... 
    // find item in database by identifier
}

Please refer here for more info.

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116