Errabi Ayoub Errabi Ayoub - 8 days ago 6
reST (reStructuredText) Question

Is @Consumes annotation required or optional in the DELETE method Jersey

I'm new in jersey rest service and I want to understand in this example the utility of adding

@Consumes
annotation to a delete method in this case this is the code it's work well (in a video ), is the
@Consumes
annotation optional here ? Thanks in advance

@path("activities")
public class ActivityResource {

@DELETE
@Path("{activityId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
public Response delete(@PathParam("activityId")String activityId) {
activityRepository.delete(activityId);
return Response.ok().build() ;
}
}

Answer

A DELETE should not be interested in anything that is in the request body. It should only identify the resource to be deleted based on the URI.

Remove the @Consumes, it is wrong here.

Also think about returning a HTTP status 204 No Content instead of 200 OK. After deleting a resource, there is nothing to return. You should also remove the @Produces because of this.

Comments