本文介绍了如何在Jersey客户端的DELETE请求中发送封装数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Jersey 2.x中,我具有以下服务器端代码:

I have the following server-side code in Jersey 2.x:

@Path("/store/remove/from/group")
@DELETE
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_PLAIN)
public Response removeStoresFromGroup(@FormParam("storeName") List<String> storeNames, @FormParam("groupName") String groupName) {
    //......
}

在客户端,我想使用Jersey 2.x客户端向上述Web服务发送删除请求.但是,从 Jersey客户端API的文档中,我没有找到如何在DELETE请求中包含以下数据:

On client side, I want to use Jersey 2.x client to send a delete request to the above web service. However, from the documentation of Jersey client API, I didn't find how to enclose the following data in DELETE request:

WebTarget webTarget = client.target("/store/remove/from/group");
MultivaluedMap<String, String> formData = new MultivaluedHashMap<String, String>();
List<String> storeName = new ArrayList<String>();
storeName.add("Store1");
storeName.add("Store2");
storeName.add("Store3");

formData.addAll("storeName", storeName);
formData.add("groupName", "Group A");

Response response = webTarget.request().accept(MediaType.TEXT_PLAIN).delete();   //The delete() method doesn't take any entity body in the request.

从Jersey客户端API中, SyncInvoker 类不支持以实体主体作为参数的delete方法.因此,我只能使用POST或PUT将数据发送到服务器,如下所示(但不能用于DELETE):

From the Jersey client API, the SyncInvoker class doesn't support a delete method with entity body as its argument. So I can only use either POST or PUT to send the data to the server like the following (but not for DELETE):

Response response = webTarget.request().accept(MediaType.TEXT_PLAIN).post(Entity.form(formData));

但是我想使用DELETE请求,因为该请求正在删除一些资源.如何通过Jersey客户端发送带有某些实体数据的DELETE请求?

But I want to use DELETE request since the request is deleting some resources. How to send DELETE request with some entity data via Jersey client?

推荐答案

带有实体主体严格禁止的DELETE ,但这是非常罕见的,并且被某些框架/服务器所忽略.对实体主体的需求可能表明DELETE未按预期使用.

A DELETE with an entity body is not strictly forbidden but it's very uncommon and ignored by some frameworks/servers. The need of an entity body may indicate that a DELETE is not used as it is intended.

例如:如果GET /customers/4711返回一个客户,而您发送DELETE /customers/4711,则此资源上的下一个GET应该返回404.您删除了由URL标识的资源 ,如在规范中定义.

For instance: If a GET /customers/4711 returns one customer and you send a DELETE /customers/4711 the next GET on this resource should return a 404. You deleted a resource identified by a URL like defined in the spec.

您的URL /store/remove/from/group似乎无法识别资源.使用/store/4711/groups/4711之类的标识符并在其上发送DELETE并不符合您的需求,因为您想从组中删除商店"而不是删除商店或组.

Your URL /store/remove/from/group does not seem to identify a resource. Using identifiers like /store/4711 or /groups/4711 and sending a DELETE on them would not fit your needs because you want to "remove a store from a group" not delete a store or a group.

假设您具有群组资源

{
  "id" : 4711,
  "stores" : [123, 456, 789]
}

,您想要一个类似

{
  "id" : 4711,
  "stores" : [123, 789]
}

您没有删除任何内容.您正在修改资源,因此PUTPOSTPATCH是适当的方法. JSON补丁是描述此类更改的好格式.请求看起来像这样:

you are not deleting anything. You are modifying a resource so PUT, POST or PATCH are appropiate methods. JSON-Patch is a good format for describing such changes. A request would look like this:

PATCH /groups/4711 HTTP/1.1
Content-Type: application/json-patch

[
  {
    "op" : "remove"
    "path" : "stores/1"
  }
]

这篇关于如何在Jersey客户端的DELETE请求中发送封装数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 05:33
查看更多