Search code examples
javarestjakarta-eejerseyjax-rs

Java EE 7 - Using @DELETE or @PUT with an entity body


I have a service method which looks like this

public void deleteData(Data data) {
    this.dataDao.deleteData(data);
}

Data class have several fields in it. Somethig like this

private String name;
private String category;
private String discriminator;
private String description;
private String appName;

// getters & setters

I need to write a rest method for this. I was thinking to write something like this

@DELETE
@Path("/deleteData")
public Response deleteData(Data data) {
    // implementation
}

The problem is that using @DELETE with entity body is not recommended or widely used.

My question is if it's ok to use @PUT instead of @DELETE? I can't change the service method implementation so that's not an option. What's the next best alternative here?

UPDATE

In dataDao.deleteData() method, finding an object is not done by object's ID. It looks something like this:

DataEntity entity = this.findDataByNameAndAppName(data.getName(), data.getAppName());

I decided to do something like this:

@DELETE
@Path("/deleteDataset")
public Response deleteDataset(@QueryParam("name") String name,
                              @QueryParam("appName") String appName) {
// implementation...
}

I didn't find any example of @DELETE method with @QueryParam, though. All examples was using @PathParam instead.


Solution

  • Well, DELETE is meant for... deleting stuff. So stick to that (without body).

    You could either delete a resource using its unique identifier sent as a path parameter:

    DELETE /resources/{id} HTTP/1.1
    Host: example.org
    

    If you need to delete multiple resources, you could consider query parameters to filter a collection of resources and then delete the resources that match such criteria:

    DELETE /resources?name=foo&category=bar HTTP/1.1
    Host: example.org