2014-04-06 55 views
0

時,從收到的迭代參數的ArrayList得到ClassCastException異常放

class Foo { 
     public String name; 
     public int age; 
    } 

public class Bar extends ServerResource { 
    @Put 
    public String update(ArrayList<Foo> foos) { 
     String names = "";   
     try { 
      for (Foo foo : foos) { 
       names += ","+foo.name; 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return names; 
    } 
} 

然而,在for循環第一行得到這個異常

java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to Foo 
    at Bar.update(Bar.java:55) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.restlet.resource.ServerResource.doHandle(ServerResource.java:503) 
    at org.restlet.resource.ServerResource.put(ServerResource.java:1275) 
    at org.restlet.resource.ServerResource.doHandle(ServerResource.java:583) 
    at org.restlet.resource.ServerResource.doNegotiatedHandle(ServerResource.java:649) 
    at org.restlet.resource.ServerResource.doConditionalHandle(ServerResource.java:348) 
    at org.restlet.resource.ServerResource.handle(ServerResource.java:952) 
    at org.restlet.resource.Finder.handle(Finder.java:246) 
    at org.restlet.routing.Filter.doHandle(Filter.java:159) 
    at org.restlet.routing.Filter.handle(Filter.java:206) 
    at org.restlet.routing.Router.doHandle(Router.java:431) 
    at org.restlet.routing.Router.handle(Router.java:648) 
    at org.restlet.routing.Filter.doHandle(Filter.java:159) 
    at org.restlet.routing.Filter.handle(Filter.java:206) 
    at org.restlet.routing.Filter.doHandle(Filter.java:159) 
    at org.restlet.routing.Filter.handle(Filter.java:206) 
    at org.restlet.routing.Filter.doHandle(Filter.java:159) 
    at org.restlet.engine.application.StatusFilter.doHandle(StatusFilter.java:155) 
    at org.restlet.routing.Filter.handle(Filter.java:206) 
    at org.restlet.routing.Filter.doHandle(Filter.java:159) 
    at org.restlet.routing.Filter.handle(Filter.java:206) 
    at org.restlet.engine.CompositeHelper.handle(CompositeHelper.java:211) 
    at org.restlet.engine.application.ApplicationHelper.handle(ApplicationHelper.java:84) 
    at org.restlet.Application.handle(Application.java:381) 
    at org.restlet.routing.Filter.doHandle(Filter.java:159) 
    at org.restlet.routing.Filter.handle(Filter.java:206) 
    at org.restlet.routing.Router.doHandle(Router.java:431) 
    at org.restlet.routing.Router.handle(Router.java:648) 
    at org.restlet.routing.Filter.doHandle(Filter.java:159) 
    at org.restlet.routing.Filter.handle(Filter.java:206) 
    at org.restlet.routing.Router.doHandle(Router.java:431) 
    at org.restlet.routing.Router.handle(Router.java:648) 
    at org.restlet.routing.Filter.doHandle(Filter.java:159) 
    at org.restlet.routing.Filter.handle(Filter.java:206) 
    at org.restlet.engine.CompositeHelper.handle(CompositeHelper.java:211) 
    at org.restlet.Component.handle(Component.java:392) 
    at org.restlet.Server.handle(Server.java:516) 
    at org.restlet.engine.ServerHelper.handle(ServerHelper.java:72) 
    at org.restlet.engine.adapter.HttpServerHelper.handle(HttpServerHelper.java:152) 
    at org.restlet.ext.servlet.ServerServlet.service(ServerServlet.java:1089) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:848) 
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:684) 
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:501) 
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137) 
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557) 
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231) 
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086) 
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:428) 
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193) 
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020) 
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135) 
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:255) 
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:154) 
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116) 
    at org.eclipse.jetty.server.Server.handle(Server.java:370) 
    at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494) 
    at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:982) 
    at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:1043) 
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:865) 
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240) 
    at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82) 
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:667) 
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52) 
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608) 
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543) 
    at java.lang.Thread.run(Thread.java:744) 

任何想法我怎麼能遍歷列表並獲取Foo項目屬性?

+1

您可以發佈你打電話給你的方法呢? 我懷疑你傳遞給你的方法參數使用下面的報頭是Accept,內容類型,其值爲「應用/ JSON」不包含富 –

+0

。我正在使用HTTP-Client來嘗試它。與PUT方法 – igx

+0

簡單的HTTP調用我沒有downvote – thobens

回答

0

標註您Foo@XmlRootElement。否則,JSON序列化程序不知道要將哪個類反序列化。

+0

同時一定要標註'更新()'用@Consumes(「應用/ JSON」) – thobens

+0

試過了 - 沒有幫助,謝謝您的好意 – igx

+0

我不知道是否能解決問題,但你嘗試過使用'列表'而不是'ArrayList的'作爲更新的'參數()'和讓框架選擇的實施? – thobens

0

解決 - 希望找到更直接的解決方案,但是這是我迄今發現

@Put 
    @Trace(dispatcher = true) 
    public String update(String args) { 
     try { 

      Gson gson = new Gson(); 
      TypeToken<List<Foo>> token = new TypeToken<List<Foo>>() { 
      }; 
      List<Foo> fooz = gson.fromJson(args, token.getType()); 
      String names = ""; 
      for (Foo foo : fooz) { 
       //do something foo.name; 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return "Success"; 
    } 
0

根據我以前的答案註釋的解決方案,這裏有一個比較合適的一個:

使用該轉換器的Restlet你需要消耗JsonRepresentation對象,並告知使用JSON資源:

@Put("json") 
public String update(JsonRepresentation foos) { 
    JSONArray fooArray = foos.getJsonArray(); 
    // ... 
} 
+0

謝謝,但org.restlet.ext.json.JsonRepresentation不帶類型參數 – igx

+0

哎呀,我迷茫與傑克遜。順便說一下,你有沒有考慮過使用Jackson來做JSON序列化?另一種選擇是JAXB ...(當然,還有更多,但我始終在我的RESTful API中使用JAXB,它的工作原理類似於魅力) – thobens

+0

謝謝,但它是較大項目的一部分 – igx

相關問題