2014-08-28 34 views
0

新手在這裏。我有一個REST服務,看起來像這樣:JerseyClient webResource問題

@GET 
@PATH("/{id}/headerinfo") 
@Produces({ JSON, XML}) 
public Response getRequestHEADER(@PathParam("id") long id) { 
    Request result = em.find(Request.class, id); 

    ... 

return Response.ok(entity).build(); 

這裏是我的電話吧,這是給我的問題:

@Path("") //what should go here? 
public class AaRestCall 
    public static String subTrackNum (String trackNum) throws IOException { 
     try { 
      Client client = Client.create(); 

      WebResource webResource = client. 
      resource("https://url/rest/request/" + trackNum); 

      ClientResponse response = webResource. 
      accept("application/json").get(ClientResponse.class); 

      String output = response.getEntity(String.class); 

      return output; 
     } 
     catch some stuff here 

} 

我有幾個問題:

1)善有善報在@Path參數?

2)webResource給我一個錯誤,它被調用爲webResource.accept時無法解析。我不清楚爲什麼。

3)任何額外的提示將不勝感激,因爲這是我第一次REST通話和第一次使用澤西島。

+0

請參閱本作pathparam例子之一:[http://stackoverflow.com/questions/24994599/expected-output-is-not - 顯示/ 24994861#24994861](http://stackoverflow.com/questions/24994599/expected-output-is-not-displaying/24994861#24994861) – 2014-08-28 15:13:54

回答

1

是裝飾類將是例如@Path("/")然後在類中的方法將是對/例如@Path("test")

+0

據我瞭解,如果uri是www.google.com/somevariable,那麼路徑將是:@Path(「www.google.com/」)? – Phant 2014-08-28 15:46:28

0

雖然這是舊的文章,我想之後的特定URI路徑參數的基本URI路徑PARAM評論一些觀點。

1)什麼在@Path參數?

據我瞭解,下面一個是你的服務器端,我的意思是服務

@GET 
@PATH("/{id}/headerinfo") 
@Produces({ JSON, XML}) 
public Response getRequestHEADER(@PathParam("id") long id) { 
    Request result = em.find(Request.class, id); 

    ... 

return Response.ok(entity).build(); 

但是第二一個是你的客戶端;

@Path("") //what should go here? 
public class AaRestCall 
    public static String subTrackNum (String trackNum) throws IOException { 
     try { 
      Client client = Client.create(); 

      WebResource webResource = client. 
      resource("https://url/rest/request/" + trackNum); 

      ClientResponse response = webResource. 
      accept("application/json").get(ClientResponse.class); 

      String output = response.getEntity(String.class); 

      return output; 
     } 
     catch some stuff here 

} 

因此,你不需要

路徑( 「」)

添加到您的客戶端類或function.Because與您的客戶端請求調用具有其自己的模式url的API,例如/service/list,因此客戶端使用該服務,這意味着不需要任何路徑除非你沒有開發一種適配器來提供2 API之間的集成(這有點兒沒有問題的概念,不要太粘)。

2)webResource給我一個錯誤,當 被調用爲webResource.accept時,webResource無法解析。我不清楚爲什麼。

基本上webResource.accept(...)的功能添加可接受的響應媒體類型,如JSON,XML文件等,因此,你應該與更多細節描述。

3)任何額外的提示,將不勝感激,因爲這是我第一次REST 電話和第一次使用澤西島。

您可以檢查用於REST框架是Jersey