afzalex afzalex - 2 months ago 36
reST (reStructuredText) Question

Pass request parameter to delete request in rest

I am using resteasy. Here is a code to delete a resource.

@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/{id:\\d+}")
public Response removeResource(@PathParam("id") int id){
.........................
.. code to delete resource and return Response object ..
.........................
}


This code is working fine. But when I try to pass some parameter to delete request. I am getting
UnsupportedMediaException


@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/{id:\\d+}")
public Response removeResource(@PathParam("id") int id, Map<String, Object> source){
.........................
.. code to delete resource and return Response object ..
.........................
}


I need to send some parameter for some reason. Moreover when I just replace
delete
request with
put
request i.e. replacing
@DELETE
with
@PUT
, the code works fine.

Is there any way to pass parameter to delete request.

And at front end I was using angularjs's $resource to send DELETE request

var r = $Resource(/rest/resources/1); // for debugging purpose I made id 1
r.remove({"key1":"data1", "key2", "data2"});


Edit :

Stack trace from server

11:43:25,767 ERROR [org.jboss.resteasy.resteasy_jaxrs.i18n] (default task-7) RESTEASY002010: Failed to execute: javax.ws.rs.NotSupportedException: RESTEASY003065: Cannot consume content type
at org.jboss.resteasy.core.registry.SegmentNode.match(SegmentNode.java:382)
at org.jboss.resteasy.core.registry.SegmentNode.match(SegmentNode.java:116)
at org.jboss.resteasy.core.registry.RootNode.match(RootNode.java:43)
at org.jboss.resteasy.core.registry.RootClassNode.match(RootClassNode.java:48)
at org.jboss.resteasy.core.ResourceMethodRegistry.getResourceInvoker(ResourceMethodRegistry.java:445)
at org.jboss.resteasy.core.SynchronousDispatcher.getInvoker(SynchronousDispatcher.java:257)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:194)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:221)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)


Response at front-end

Status Code: 415 Unsupported Media Type
Connection: keep-alive
Content-Length: 0
Date: Wed, 01 Jun 2016 06:13:25 GMT
Server: WildFly/10
x-powered-by: Undertow/1

Tim Tim
Answer

You have requested that the Content-Type be "application/json" AngularJS defaults to text/plain.

If you have a new enough version of AngularJS(1.1.3) you can customise the resource object to include the Content-Type you requested. You should be able to modify your resource definition to include the Content-Type for a delete request

var r = $Resource(/rest/resources/1, {},
   remove:{
      method:"DELETE",
      isArray:false,
      headers:{'Content-Type':'application/json; charset=UTF-8'} 
   }
);

For more info see Answer One and Angular issue