Search code examples
javarestspring-mvcpath-variables

How to get pathvariables from a REST URL?


I have the URL path

/customer/7854/order/125

How do I get the customerID 7854 and the orderID 125? Currently I'm handling this by splitting the complete string but I don't like it.

Are there alternatives?


Solution

  • If you using Spring REST, you can use @PathVariable("id") long id. eg for multiple params:

    @RequestMapping(value = "/ex/foos/{fooid}/bar/{barid}")
    @ResponseBody
    public String getPathValues
      (@PathVariable long fooid, @PathVariable long barid) {
        return "Get a specific Bar with id=" + barid + " from a Foo with id=" + fooid;
    }