Reputation: 345
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?
Upvotes: 2
Views: 2703
Reputation: 5938
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;
}
Upvotes: 5
Reputation: 27356
As you said in the comments, you're using Spring MVC.
Luckily for you, it provides a really nice way of mapping URL parameters to values!
@RequestMapping(value = "/{bookmarkId}", method = RequestMethod.GET)
As you can see, on the action, you can specify the bookmarkId
. And then you specify the parameter..
Bookmark readBookmark(@PathVariable Long bookmarkId)
And now you can access bookmarkId
as if it was any other value.
Edit
So the whole thing together, taken from the examples, would look like this:
@RequestMapping(value = "/{bookmarkId}", method = RequestMethod.GET)
Bookmark readBookmark(@PathVariable String userId, @PathVariable Long bookmarkId) {
this.validateUser(userId);
return this.bookmarkRepository.findOne(bookmarkId);
}
Upvotes: 1