2016-06-01 56 views
1

我正在使用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 .. 
    ......................... 
} 

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

有没有办法通过参数来删除请求。

而在前端我使用angularjs的$资源发送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

您可以显示完整的堆栈跟踪 –

+0

还有些服务器不用DELETE支持一个主体。尽管'UnsupportedMediaException'听起来像一个RESTEasy异常,所以错误发生在应用程序级别。我不太确定RESTEasy有关这方面的规则。最坏的情况下,只发送查询参数中的数据 –

+0

@peeskillet我在前端添加了web服务器和响应的stacktrace – afzalex

回答

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 OneAngular issue