小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

@QueryParam跟@PathParam比較

 _鎏_ 2015-08-04

來(lái)源:http://jackyrong./blog/1128364

1 先來(lái)看@queryparam
   先看例子:
 

Java代碼  收藏代碼
  1. Path("/users")  
  2. public class UserService {  
  3.    
  4.     @GET  
  5.     @Path("/query")  
  6.     public Response getUsers(  
  7.         @QueryParam("from"int from,  
  8.         @QueryParam("to"int to,  
  9.         @QueryParam("orderBy") List<String> orderBy) {  
  10.    
  11.         return Response  
  12.            .status(200)  
  13.            .entity("getUsers is called, from : " + from + ", to : " + to  
  14.             + ", orderBy" + orderBy.toString()).build();  
  15.    
  16.     }  
  17.    
  18. }  


   URL輸入為:users/query?from=100&to=200&orderBy=age&orderBy=name
  此時(shí),輸出為:
getUsers is called, from : 100, to : 200, orderBy[age, name]
   要注意的是,跟@pathparam不同,@queryparam
中,指定的是URL中的參數(shù)是以鍵值對(duì)的形式出現(xiàn)的,而在程序中
@QueryParam("from") int from則讀出URL中from的值,
而@pathparem中,URL中只出現(xiàn)參數(shù)的值,不出現(xiàn)鍵值對(duì),比如:
“/users/2011/06/30”

則:
 

Java代碼  收藏代碼
  1. @GET  
  2.     @Path("{year}/{month}/{day}")  
  3.     public Response getUserHistory(  
  4.             @PathParam("year"int year,  
  5.             @PathParam("month"int month,   
  6.             @PathParam("day"int day) {  
  7.    
  8.        String date = year + "/" + month + "/" + day;  
  9.    
  10.        return Response.status(200)  
  11.         .entity("getUserHistory is called, year/month/day : " + date)  
  12.         .build();  
  13.    
  14.     }  


輸出為:
getUserHistory is called, year/month/day : 2011/6/30

2 以動(dòng)態(tài)的方式獲得:
  

Java代碼  收藏代碼
  1. @Path("/users")  
  2. public class UserService {  
  3.    
  4.     @GET  
  5.     @Path("/query")  
  6.     public Response getUsers(@Context UriInfo info) {  
  7.    
  8.         String from = info.getQueryParameters().getFirst("from");  
  9.         String to = info.getQueryParameters().getFirst("to");  
  10.         List<String> orderBy = info.getQueryParameters().get("orderBy");  
  11.    
  12.         return Response  
  13.            .status(200)  
  14.            .entity("getUsers is called, from : " + from + ", to : " + to  
  15.             + ", orderBy" + orderBy.toString()).build();  
  16.    
  17.     }  
  18.    



URL;users/query?from=100&to=200&orderBy=age&orderBy=name
輸出為:
getUsers is called, from : 100, to : 200, orderBy[age, name]
注意這里把orderby后的兩個(gè)參數(shù)讀入為L(zhǎng)IST處理了.


3 @DefaultValue,默認(rèn)值

  例子:
 

Java代碼  收藏代碼
  1. @Path("/users")  
  2. public class UserService {  
  3.    
  4.     @GET  
  5.     @Path("/query")  
  6.     public Response getUsers(  
  7.         @DefaultValue("1000"@QueryParam("from"int from,  
  8.         @DefaultValue("999")@QueryParam("to"int to,  
  9.         @DefaultValue("name"@QueryParam("orderBy") List<String> orderBy) {  
  10.    
  11.         return Response  
  12.            .status(200)  
  13.            .entity("getUsers is called, from : " + from + ", to : " + to  
  14.             + ", orderBy" + orderBy.toString()).build();  
  15.    
  16.     }  



URL:users/query
輸出:getUsers is called, from : 1000, to : 999, orderBy[name]

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多