2016-06-01 250 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