JBoss Resteasy 2之URL匹配

來源:互聯網
上載者:User

在上一篇helloworld中,簡單介紹了入門,本文講解其URL匹配,也是REST
中很重要的一環
看例子:

Java代碼
  1. @Path("/users")   
  2. public class UserRestService {   
  3.     
  4.     @GET  
  5.     @Path("{name}")   
  6.     public Response getUserByName(@PathParam("name") String name) {   
  7.     
  8.         return Response.status(200)   
  9.             .entity("getUserByName is called, name : " + name).build();   
  10.     
  11.     }   
  12.     @GET  
  13.     @Path("{id : \\d+}") //support digit only
      
  14.     public Response getUserById(@PathParam("id") String id) {   
  15.     
  16.        return Response.status(200).entity("getUserById is called, id : " + id).build();   
  17.     
  18.     }   
  19.     
  20.     @GET  
  21.     @Path("/username/{username : [a-zA-Z][a-zA-Z_0-9]}")   
  22.     public Response getUserByUserName(@PathParam("username") String username) {   
  23.     
  24.        return Response.status(200)   
  25.         .entity("getUserByUserName is called, username : " + username).build();   
  26.     
  27.     }   
  28.     
  29.     @GET  
  30.     @Path("/books/{isbn : \\d+}")   
  31.     public Response getUserBookByISBN(@PathParam("isbn") String isbn) {   
  32.     
  33.        return Response.status(200)   
  34.         .entity("getUserBookByISBN is called, isbn : " + isbn).build();   
  35.     
  36.     }  
@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(); }

 

可以看到,還支援Regex。所以,容易看出:

1) “/users/999”
返回:getUserById is called, id : 999

2) /users/username/aaa”
  不匹配

3) users/books/999”
  返回:getUserBookByISBN is called, isbn : 999

再看例子:
 

Java代碼
  1. @Path("/users")   
  2. public class UserRestService {   
  3.     
  4.     @GET  
  5.     @Path("{id}")   
  6.     public Response getUserById(@PathParam("id") String id) {   
  7.     
  8.        return Response.status(200).entity("getUserById is called, id : " + id).build();   
  9.     
  10.     }   
  11.    
@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(); } 

則“/users/22667788 匹配
  getUserById is called, id : 22667788

多個參數傳入的例子:
 

Java代碼
  1. @Path("/users")   
  2. public class UserRestService {   
  3.     
  4.     @GET  
  5.     @Path("{year}/{month}/{day}")   
  6.     public Response getUserHistory(   
  7.             @PathParam("year") int year,   
  8.             @PathParam("month") int month,    
  9.             @PathParam("day") int day) {   
  10.     
  11.        String date = year + "/" + month + "/" + day;   
  12.     
  13.        return Response.status(200)   
  14.         .entity("getUserHistory is called, year/month/day : " + date)   
  15.         .build();   
  16.     
  17.     }   
  18.    
@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(); } 

則:
“/users/2011/06/30”
輸出:
   getUserHistory is called, year/month/day : 2011/6/30 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.