【轉】@javax.ws.rs Webservice註解

來源:互聯網
上載者:User

標籤:環境   oar   nts   script   取值   ddr   blog   anim   str   

用於webservice。

1.路徑

@javax.ws.rs.Path

標識要請求的資源類或資源方法的uri路徑。
例,@Path("animal"),表示下一層路徑是animal時要處理的事務。
@Path("{species}")這種帶大括弧的表示方法,表示下一層路徑會被參數化,配合@PathParam("species")使用可以賦值給函數的參數。

對於代碼:

[java] view plain copy print?
  1. @Path("animal")  
  2. public class Animal {  
  3.     public String species,name;  
  4.     public int age;  
  5.     public static Animal animal=new Animal();  
  6.       
  7.     @GET  
  8.     @Path("{species}")  
  9.     @Produces(MediaType.APPLICATION_JSON)  
  10.     public Animal wsAnimal(@PathParam("species") String species,  
  11.             @QueryParam("name") String name,  
  12.             @QueryParam("age") int age  
  13.             ){  
  14.         animal.species=species;  
  15.         animal.name=name;  
  16.         animal.age=  age==0?2:age;  
  17.         return animal;  
  18.     }  
  19. }  
@Path("animal")public class Animal {public String species,name;public int age;public static Animal animal=new Animal();@GET@Path("{species}")@Produces(MediaType.APPLICATION_JSON)public Animal wsAnimal(@PathParam("species") String species,@QueryParam("name") String name,@QueryParam("age") int age){animal.species=species;animal.name=name;animal.age=  age==0?2:age;return animal;}}


效果見圖1-1:

圖 1-1 @Path 用法樣本

 

2.從報文資料到方法參數

@javax.ws.rs.PathParam

將uri中指定的路徑參數綁定到資源方法參數,資源類的欄位,或資源類的bean屬性。

@javax.ws.rs.QueryParam

將http請求的Query參數賦值給函數的參數。

@javax.ws.rs.DefaultValue

設定@QueryParam參數的預設值。如果@QueryParam沒有接收到值,就使用預設值。比如:

[java] view plain copy print?
  1. public String fun(@DefaultValue("description") @QueryParam("desc") String desc){...}  
public String fun(@DefaultValue("description") @QueryParam("desc") String desc){...}

@javax.ws.rs.FormParam

將http請求的Form表單中的參數賦值給函數的參數。

@avax.ws.rs.Context

用於擷取環境資訊。一個擷取用戶端ip的例子見下:

[java] view plain copy print?
  1. @Path("util")  
  2. public class Util {  
  3.       
  4.     @Path("getClientIp")  
  5.     @GET  
  6.     @Produces(MediaType.TEXT_PLAIN)  
  7.     public String getClientIp(@Context HttpServletRequest request){  
  8.         return request.getRemoteAddr();  
  9.     }  
  10. }  
@Path("util")public class Util {@Path("getClientIp")@GET@Produces(MediaType.TEXT_PLAIN)public String getClientIp(@Context HttpServletRequest request){return request.getRemoteAddr();}}

 

3.http方法

@javax.ws.rs.GET
表示此方法響應一個HTTP GET請求。

@javax.ws.rs.POST
表示此方法響應一個HTTP POST請求。

@javax.ws.rs.PUT

通常用來更新資料。

@javax.ws.rs.DELETE

通常用來刪除資料。

4.返回資料類型

@javax.ws.rs.Produces
設定Http返回報文,報文體的內容類型。
取值為 javax.ws.rs.core.MediaType.XXX。常用的有:

MediaType.APPLICATION_JSON
MediaType.TEXT_PLAIN

【轉】@javax.ws.rs Webservice註解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.