简体   繁体   English

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

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

I am trying to create a web API using Jersey.我正在尝试使用 Jersey 创建一个 Web API。 I am trying to run a method similar to this:我正在尝试运行类似于此的方法:

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

However Jersey 2.x does not have a WebResource or Resource class.但是 Jersey 2.x 没有WebResourceResource类。 So what class can I use instead in order to have the uri http://localhost:8080/Jersey/rest/contacts as a parameter?那么我可以使用什么类来将 uri http://localhost:8080/Jersey/rest/contacts作为参数? This will be ran in a ContactClient class这将在ContactClient类中运行

Have a look at the Client API from the Jersey documentation.查看 Jersey 文档中的客户端 API With Jersey 2.x you instead want to use WebTarget .在 Jersey 2.x 中,您想要使用WebTarget For example例如

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

See the documentation I linked to for much more information and examples.有关更多信息和示例,请参阅我链接到的文档。

JAX-RS 2.0 Client API : JAX-RS 2.0 introduces a new client API so that you can make http requests to your remote RESTful web services. JAX-RS 2.0 客户端 API :JAX-RS 2.0 引入了一个新的客户端 API,以便您可以向远程RESTful Web 服务发出http 请求

It is a 'fluent' request building API with really 3 main classes:这是一个“流畅”的请求构建 API,具有真正的3 个主要类:

  1. Client,客户,
  2. WebTarget, and WebTarget 和
  3. Response.回复。

1. Making a simple client request 1. 做一个简单的客户端请求

Jersey 1.x way: 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 way: 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. Attaching entity to request 2. 附加实体请求

Jersey 1.x way: Jersey 1.x 方式:

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

JAX-RS 2.0 way: 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