I'm currently facing a simple idea with a API that looks like this:
/users -> return a collection of users
/users/1 -> return all data included in one user
/users/1/information -> return the information json
/users/1/dogs/rex -> return the information of the users dog rex :)
/users/1/dogs/ -> return the collection of the users dogs
Note these represent a folderstructure in a file system where users, 1, dogs, rex are folders and information would be file including a json (doesn't really matter what is in it)
So my idea was building this with a regex (why?! I want to make this a little generic - all I'm doing here is only "a little figment").
1 Folder: \/[a-zA-Z0-9]+
2 Folders: \/[a-zA-Z0-9]+\/[a-zA-Z0-9]+
2 Folders - 1 File: \/[a-zA-Z0-9]+\/[a-zA-Z0-9]+\/[a-zA-Z0-9]+
4 Folders: \/[a-zA-Z0-9]+\/[a-zA-Z0-9]+\/[a-zA-Z0-9]+\/[a-zA-Z0-9]+ //should be the first loop of 2 folders.
So how do I get a regex does also match a loop (i know 2 Folders *3 -> 6 Folders)
and (2 Folders + 1 File)*2 -> 6 "Folders")
but at this point it doesn't really matter for me.
If you want to model folders and files and their relationship, I propose a different approach. It is not necassary to duplicate the file/folder relationshipt in the URLs. Such information can be part of the representation of each file/folder resource.
A File has an ID, a name, and possibly some other properties. It is contained in a folder.
{
"id": "file-1",
"link": "/files/file-1",
"name": "file-1.txt",
"folder": "folder-1"
}
The collection resource of all files has the URL
/files
A single file has the URL
/files/{id}
for example
/files/file-1
A folder has an ID, a name, and possibly some other properties. It contains zero or more files and folders.
{
"id": "folder-1",
"link": "/folders/folder-1",
"name": "folder-1",
"children": [
{
"id": "folder-2",
"link": "/folders/folder-2"
},
{
"id": "file-1"
"link": "/files/file-1"
}
]
}
The collection resource of all folders has the URL
/folders
A single folder has the URL
/folders/{id}
for example
/folders/folder-1
The root folder has some known ID, for examle
/folders/root
I know this isn't what you wanted but rather than try and shoe-horn your solution into regex, why don't you capture the whole path with @PathParam
and parse it out in java?
@Path("/operation/{path:.+}")
public String getFile(@PathParam("path") String path) {
// Parse "path"
}
The path parameter shown should capture everything after the /operation/ in the rest call.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.