java restful web services @path regular expression
1
The following example shows how regular expression could be used with the @Path annotation.
@Path("users/{username: regular expression}")
Example:
@Path("users/{username: [a-zA-Z][a-zA-Z_0-9]}")
following URL: http://example.com/users/hello
2
The @PathParam annotation example
@Path("/users/{username}")
public class User {
@GET
@Produces("text/xml")
public String getUser(@PathParam("username") String userName) {
System.out.println(userName);
}
}
following URL: http://example.com/users/hello
The following example shows how regular expression could be used with the @Path annotation.
@Path("users/{username: regular expression}")
Example:
@Path("users/{username: [a-zA-Z][a-zA-Z_0-9]}")
following URL: http://example.com/users/hello
2
The @PathParam annotation example
@Path("/users/{username}")
public class User {
@GET
@Produces("text/xml")
public String getUser(@PathParam("username") String userName) {
System.out.println(userName);
}
}
following URL: http://example.com/users/hello
3
Ignore any character in the @Path.
@GET
@Path("/report/download/file/{filename : .+}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response downloadFilebyPath(@PathParam("filename") String fileName) {
System.out.println("file location:--- //"+fileName);
}
following URL: http://example.com/users/xxxx/xxxxxx/xxxxx/xxxxx.xxx
{filename : .+} this will accept any character.
When you are sending any file complete path or directory name in the URL that contain '/ or \ or any symbol' this logic is Useful.
Ignore any character in the @Path.
@GET
@Path("/report/download/file/{filename : .+}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response downloadFilebyPath(@PathParam("filename") String fileName) {
System.out.println("file location:--- //"+fileName);
}
following URL: http://example.com/users/xxxx/xxxxxx/xxxxx/xxxxx.xxx
{filename : .+} this will accept any character.
When you are sending any file complete path or directory name in the URL that contain '/ or \ or any symbol' this logic is Useful.
Comments
Post a Comment