JBoss resteasy 2 URL matching

Source: Internet
Author: User
Tags jboss

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
  1. @ Path ("/users ")
  2. Public class userrestservice {
  3. @ Get
  4. @ Path ("{name }")
  5. Public Response getuserbyname (@ pathparam ("name") string name ){
  6. Return response. Status (200)
  7. . Entity ("getuserbyname is called, name:" + name). Build ();
  8. }
  9. @ Get
  10. @ Path ("{ID: \ D +}") // support digit only
  11. Public Response getuserbyid (@ pathparam ("ID") string ID ){
  12. Return response. Status (200). Entity ("getuserbyid is called, ID:" + id). Build ();
  13. }
  14. @ Get
  15. @ Path ("/username/{Username: [A-Za-Z] [a-zA-Z_0-9]}")
  16. Public Response getuserbyusername (@ pathparam ("username") string username ){
  17. Return response. Status (200)
  18. . Entity ("getuserbyusername is called, Username:" + username). Build ();
  19. }
  20. @ Get
  21. @ Path ("/books/{ISBN: \ D + }")
  22. Public Response getuserbookbyisbn (@ pathparam ("ISBN") string ISBN ){
  23. Return response. Status (200)
  24. . Entity ("getuserbookbyisbn is called, ISBN:" + ISBN). Build ();
  25. }
@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
  1. @ Path ("/users ")
  2. Public class userrestservice {
  3. @ Get
  4. @ Path ("{ID }")
  5. Public Response getuserbyid (@ pathparam ("ID") string ID ){
  6. Return response. Status (200). Entity ("getuserbyid is called, ID:" + id). Build ();
  7. }
@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
  1. @ Path ("/users ")
  2. Public class userrestservice {
  3. @ Get
  4. @ Path ("{year}/{month}/{day }")
  5. Public Response getuserhistory (
  6. @ Pathparam ("year") int year,
  7. @ Pathparam ("month") int month,
  8. @ Pathparam ("day") int day ){
  9. String date = year + "/" + month + "/" + Day;
  10. Return response. Status (200)
  11. . Entity ("getuserhistory is called, year/month/day:" + date)
  12. . Build ();
  13. }
@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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.