Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send enclose data in DELETE request in Jersey client?

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) {
    //......
}

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.

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)); 

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?

like image 880
tonga Avatar asked Aug 10 '14 15:08

tonga


1 Answers

You can use webTarget.request().accept(MediaType.TEXT_PLAIN).method("DELETE",yourEntity) to invoke a DELETE with an entity in it.

like image 116
John Ament Avatar answered Oct 12 '22 02:10

John Ament