簡體   English   中英

Jersey 2.x 不包含 WebResource 和資源類。 我可以用什么代替?

[英]Jersey 2.x does not contain WebResource and resource class. What can I use instead?

我正在嘗試使用 Jersey 創建一個 Web API。 我正在嘗試運行類似於此的方法:

WebResource r = c.resource("http://localhost:8080/Jersey/rest/contacts");

但是 Jersey 2.x 沒有WebResourceResource類。 那么我可以使用什么類來將 uri http://localhost:8080/Jersey/rest/contacts作為參數? 這將在ContactClient類中運行

查看 Jersey 文檔中的客戶端 API 在 Jersey 2.x 中,您想要使用WebTarget 例如

Client client = ClientBuilder.newClient();
WebTarget target = client.target(url);
Response response = target.request().get();

有關更多信息和示例,請參閱我鏈接到的文檔。

JAX-RS 2.0 客戶端 API :JAX-RS 2.0 引入了一個新的客戶端 API,以便您可以向遠程RESTful Web 服務發出http 請求

這是一個“流暢”的請求構建 API,具有真正的3 個主要類:

  1. 客戶,
  2. WebTarget 和
  3. 回復。

1. 做一個簡單的客戶端請求

Jersey 1.x 方式:

Client client = Client.create();
  WebResource webResource = client.resource(restURL).path("myresource/{param}");
  String result = webResource.pathParam("param", "value").get(String.class);

JAX-RS 2.0 方式:

Client client = ClientFactory.newClient();
 WebTarget target = client.target(restURL).path("myresource/{param}");
 String result = target.pathParam("param", "value").get(String.class);

2. 附加實體請求

Jersey 1.x 方式:

Client client = Client.create();
 WebResource webResource = client.resource(restURL);
 ClientResponse response = webResource.post(ClientResponse.class, "payload");

JAX-RS 2.0 方式:

Client client = ClientFactory.newClient();
 WebTarget target = client.target(restURL);
 Response response = target.request().post(Entity.text("payload"), Response.class);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM