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

分享

RestEasy+用戶指南----第5章.@PathParam

 CevenCheng 2011-12-14
 

RestEasy+用戶指南----第5章.@PathParam

分類: WebService 翻譯 102人閱讀 評論(0) 收藏 舉報

@PathParam 的聲明允許你在URI路徑中去映射你的方法將使用的參數(shù)。


  1. @Path("/library")  
  2. public class Library {  
  3.   
  4.    @GET  
  5.    @Path("/book/{isbn}")  
  6.    public String getBook(@PathParam("isbn") String id) {  
  7.       // search my database and get a string representation and return it  
  8.    }  
  9. }  

(很簡單,當(dāng)你發(fā)出get請求 /book/152-963參數(shù)152-963就在isbn中存儲著,然后交給變量id,這樣你的方法就算是成功的接收了該參數(shù))

這將允許你在uri中內(nèi)嵌一個變量標(biāo)識符。在上邊的例子中,參數(shù)isbn被用來傳遞book的信息。你所嵌入的數(shù)據(jù)類型可以是任何元數(shù)據(jù)類型,例如String,具有String參數(shù)的構(gòu)造函

數(shù)的一個類對象,或者a static valueOf method that takes a String as a parameter。例如,假設(shè)ISBN是一個對象,我們可以

  1. @GET  
  2. @Path("/book/{isbn}")  
  3. public String getBook(@PathParam("isbn") ISBN id) {...}  
  4.   
  5.   
  6. public class ISBN {  
  7.    public ISBN(String str) {...}  
  8. }  

或者是一個public方法String構(gòu)造,包含一個valueOf 方法


  1. <span style="font-size:16px;">  public class ISBN {  
  2.        
  3.      public static ISBN valueOf(String isbn) {...}  
  4.   }</span>  

(運行中應(yīng)該能夠自動調(diào)用類的valueOf方法進(jìn)行轉(zhuǎn)換,對java不是很熟悉,我想大概應(yīng)該是這樣)


5.1. @PathParam深入 以及正則表達(dá)式


下邊是一些更復(fù)雜的應(yīng)用,這些在前邊的章節(jié)并沒有討論

你可以指定一個或者多個參數(shù)用以內(nèi)嵌到你的uri中,下邊是一些例子

1.@Path("/aaa{param}bbb")

2.@Path("/{name}-{zip}")

3.@Path("/foo{name}-{zip}bar")


那么,路徑 "/aaa111bbb" 將會匹配#1. "/bill-02115"將會匹配 #2. 路徑"foobill-02115bar" 將會匹配 #3.

之前,我們已經(jīng)討論過如何在@Path中使用正則表達(dá)式

  1. @GET  
  2. @Path("/aaa{param:b+}/{many:.*}/stuff")  
  3. public StringgetIt(@PathParam("param") String bs, @PathParam("many")String many) {...}  

在如下的請求中,我們可以了解到“param”以及“many”值是多少

Request

param

many

GET /aaabb/some/stuff

bb

some

GET /aaab/a/lot/of/stuff

b

a/lot/of


5.2@PathParam 和 PathSegment

Thespecification has a very simple abstraction for examining a fragment of the URIpath being invoked on javax.ws.rs.core.PathSegment:

  1. public interface PathSegment {  
  2.   
  3.     /** 
  4.      * Get the path segment. 
  5.      * <p> 
  6.      * @return the path segment 
  7.      */  
  8.     String getPath();  
  9.     /** 
  10.      * Get a map of the matrix parameters associated with the path segment 
  11.      * @return the map of matrix parameters 
  12.      */  
  13.     MultivaluedMap<String, String> getMatrixParameters();  
  14.       
  15. }  

你可以使用resteasy注入一個PathSegment而不是用一個值

  1. @GET  
  2. @Path("/book/{id}")  
  3. public String getBook(@PathParam("id") PathSegment id) {...}  

當(dāng)你使用matrix parameters傳遞諸多參數(shù)時,浙江愛那個非常有用。你可以將任意個name和value的鍵值對潛入到uri path segment中。PathSegment對象將會負(fù)責(zé)去獲取這些參數(shù)。

你也可以看一下MatrixParam(后邊會講到)

一個matrix parameter的例子是

 

GEThttp:///library/book;name=EJB 3.0;author=Bill Burke

 

Thebasic idea of matrix parameters is that it represents resources that areaddressable by their attributes as well as their raw id.

(好吧,5.2我表示不怎么理解,自己還沒有試過這個部分。)

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

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多