In the previous article helloworld, I briefly introduced the entry point. This article describes its URL matching, which is also a rest
An important part
Example:
Java code
- @ Path ("/users ")
- Public class userrestservice {
- @ Get
- @ Path ("{name }")
- Public Response getuserbyname (@ pathparam ("name") string name ){
- Return response. Status (200)
- . Entity ("getuserbyname is called, name:" + name). Build ();
- }
- @ Get
- @ Path ("{ID: \ D +}") // support digit only
- Public Response getuserbyid (@ pathparam ("ID") string ID ){
- Return response. Status (200). Entity ("getuserbyid is called, ID:" + id). Build ();
- }
- @ Get
- @ Path ("/username/{Username: [A-Za-Z] [a-zA-Z_0-9]}")
- Public Response getuserbyusername (@ pathparam ("username") string username ){
- Return response. Status (200)
- . Entity ("getuserbyusername is called, Username:" + username). Build ();
- }
- @ Get
- @ Path ("/books/{ISBN: \ D + }")
- Public Response getuserbookbyisbn (@ pathparam ("ISBN") string ISBN ){
- Return response. Status (200)
- . Entity ("getuserbookbyisbn is called, ISBN:" + ISBN). Build ();
- }
@Path("/users")public class UserRestService { @GET@Path("{name}")public Response getUserByName(@PathParam("name") String name) { return Response.status(200).entity("getUserByName is called, name : " + name).build(); }@GET@Path("{id : \\d+}") //support digit onlypublic Response getUserById(@PathParam("id") String id) { return Response.status(200).entity("getUserById is called, id : " + id).build(); } @GET@Path("/username/{username : [a-zA-Z][a-zA-Z_0-9]}")public Response getUserByUserName(@PathParam("username") String username) { return Response.status(200).entity("getUserByUserName is called, username : " + username).build(); } @GET@Path("/books/{isbn : \\d+}")public Response getUserBookByISBN(@PathParam("isbn") String isbn) { return Response.status(200).entity("getUserBookByISBN is called, isbn : " + isbn).build(); }
We can see that regular expressions are also supported. Therefore, it is easy to see:
1) "/users/999"
Return Value: getuserbyid is called, ID: 999
2)/users/username/AAA"
Mismatch
3) users/books/999"
Return Value: getuserbookbyisbn is called, ISBN: 999
Let's look at the example:
Java code
- @ Path ("/users ")
- Public class userrestservice {
- @ Get
- @ Path ("{ID }")
- Public Response getuserbyid (@ pathparam ("ID") string ID ){
- Return response. Status (200). Entity ("getuserbyid is called, ID:" + id). Build ();
- }
@Path("/users")public class UserRestService { @GET@Path("{id}")public Response getUserById(@PathParam("id") String id) { return Response.status(200).entity("getUserById is called, id : " + id).build(); }
Then "/users/22667788" matches
Getuserbyid is called, ID: 22667788
Example of multiple parameter input:
Java code
- @ Path ("/users ")
- Public class userrestservice {
- @ Get
- @ Path ("{year}/{month}/{day }")
- Public Response getuserhistory (
- @ Pathparam ("year") int year,
- @ Pathparam ("month") int month,
- @ Pathparam ("day") int day ){
- String date = year + "/" + month + "/" + Day;
- Return response. Status (200)
- . Entity ("getuserhistory is called, year/month/day:" + date)
- . Build ();
- }
@Path("/users")public class UserRestService { @GET@Path("{year}/{month}/{day}")public Response getUserHistory(@PathParam("year") int year,@PathParam("month") int month, @PathParam("day") int day) { String date = year + "/" + month + "/" + day; return Response.status(200).entity("getUserHistory is called, year/month/day : " + date).build(); }
Then:
"/Users/2011/06/30"
Output:
Getuserhistory is called, year/month/day: 2011/6/30