[英]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 没有WebResource
或Resource
类。 那么我可以使用什么类来将 uri http://localhost:8080/Jersey/rest/contacts
作为参数? 这将在ContactClient
类中运行
JAX-RS 2.0 客户端 API :JAX-RS 2.0 引入了一个新的客户端 API,以便您可以向远程RESTful Web 服务发出http 请求。
这是一个“流畅”的请求构建 API,具有真正的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.