传递请求参数以在 rest 中删除请求

Pass request parameter to delete request in rest

我正在使用resteasy。这是删除资源的代码。

@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 ..
    .........................
}

此代码运行良好。但是当我尝试传递一些参数来删除请求时。我得到 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 ..
    .........................
}

出于某种原因,我需要发送一些参数。此外,当我将 delete 请求替换为 put 请求时,即将 @DELETE 替换为 @PUT,代码工作正常。

删除请求有没有传参的方法

并且在前端我使用 angularjs 的 $resource 发送删除请求

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

编辑:
来自服务器的堆栈跟踪

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)

前端响应

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

您已请求 Content-Type 为 "application/json" AngularJS defaults 至 text/plain。

如果您有足够新的 AngularJS(1.1.3) 版本,您可以自定义资源对象以包含您请求的内容类型。 您应该能够修改您的资源定义以包含删除请求的内容类型

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

有关详细信息,请参阅 Answer One and Angular issue