I am trying to create a web API using Jersey. 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. So what class can I use instead in order to have the uri http://localhost:8080/Jersey/rest/contacts
as a parameter? This will be ran in a ContactClient
class
Have a look at the Client API from the Jersey documentation. With Jersey 2.x you instead want to use 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 introduces a new client API so that you can make http requests to your remote RESTful web services. :JAX-RS 2.0 引入了一个新的客户端 API,以便您可以向远程RESTful Web 服务发出http 请求。
It is a 'fluent' request building API with really 3 main classes:
Client client = Client.create();
WebResource webResource = client.resource(restURL).path("myresource/{param}");
String result = webResource.pathParam("param", "value").get(String.class);
Client client = ClientFactory.newClient();
WebTarget target = client.target(restURL).path("myresource/{param}");
String result = target.pathParam("param", "value").get(String.class);
Client client = Client.create();
WebResource webResource = client.resource(restURL);
ClientResponse response = webResource.post(ClientResponse.class, "payload");
Client client = ClientFactory.newClient();
WebTarget target = client.target(restURL);
Response response = target.request().post(Entity.text("payload"), Response.class);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.