0

Can I use @QueryParam when defining a DELETE operation? If yes, when I need to test it using jersey client, how the java code will look like? I tried something like

String result = client.target(url)
                      .queryParam("id",3)
                      .request(MediaType.APPLICATION_XML)
                      .delete(String.class);

But it was not working.

Bentaye
  • 9,403
  • 5
  • 32
  • 45
vineeth
  • 198
  • 3
  • 13
  • It would be better design to have the id in the path, ie. `DELETE /url/:id` and use `@PathParam`. If you can change it, I suggest you do. – Bentaye Mar 16 '18 at 10:59
  • @Bentaye Ok. So when we need to use QueryParam? – vineeth Mar 16 '18 at 12:05
  • This is a whole new question regarding RESTapi design , I recommend you simply google it you will find a lot of answers. this for example https://stackoverflow.com/questions/11552248/when-to-use-queryparam-vs-pathparam – Bentaye Mar 16 '18 at 12:13

1 Answers1

0

You can define a DELETE endpoint using either @PathParam (recommended) or @QueryParam

With @PathParam (recommended)

Endpoint: url/:id

@DELETE
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public void deletePathParam(@PathParam("id") int id) {
  ...
}

Test:

@Test
public void testDeletePathParam() {
  Response output = target(url+"/3").request().delete();
   // asserts
}

With @QueryParam (bad RESTapi design)

Endpoint: url?id=:id

@DELETE
@Produces(MediaType.APPLICATION_JSON)
public void deleteQueryParam(@QueryParam("id") int id) {
  ...
}

Test:

@Test
public void testDeleteQueryParam() {
  Response output = target(url).queryParam("id", 3).request().delete();
   // asserts
}
Bentaye
  • 9,403
  • 5
  • 32
  • 45