我正在使用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发送DELETE请求
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
答案 0 :(得分:1)
您已请求Content-Type为“application / json” AngularJS defaults到text / plain。
如果您有足够新版本的AngularJS(1.1.3),则可以自定义资源对象以包含您请求的Content-Type。 您应该能够修改资源定义以包含删除请求的Content-Type
var r = $Resource(/rest/resources/1, {},
remove:{
method:"DELETE",
isArray:false,
headers:{'Content-Type':'application/json; charset=UTF-8'}
}
);
有关详细信息,请参阅Answer One和Angular issue