如何使用serialize Collections.synchronizedList和co
我是网络服务的初学者。我试图编写 Restful webservice,然后我遵循了 http://www.stupidjavatricks.com/?p=54。这个例子对我有用。但是当我尝试在我的代码库上实现相同的功能时,它失败了,并出现以下异常:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.oxm.MarshallingFailureException: XStream marshalling exception; nested exception is com.thoughtworks.xstream.converters.ConversionException: Could not call java.util.concurrent.ConcurrentHashMap.writeObject() : Could not call java.util.Collections$SynchronizedList.writeObject() : Cannot reference implicit element
---- Debugging information ----
implicit-element : [EasyMock for class com.pyramid.qls.computeinvoker.Task]
referencing-element : /batchProgressMetrics/batchProgressUpdater/context/servletContext/context/attributes/java.util.concurrent.ConcurrentHashMap/org.springframework.web.context.support.XmlWebApplicationContext/servletConfig/config/parent/lifecycle/listeners/org.apache.catalina.core.StandardHost_-MemoryLeakTrackingListener/outer-class/listeners/org.apache.catalina.connector.MapperListener/connector/service/connectors/org.apache.catalina.connector.Connector[2]/container/thread/group/threads/java.lang.Thread/threadLocals/table/java.lang.ThreadLocal_-ThreadLocalMap_-Entry[11]/value/org.easymock.classextension.internal.ClassProxyFactory_-1/val_-handler/delegate/control/state/behavior/behaviorLists/org.easymock.internal.UnorderedBehavior/results/org.easymock.internal.ExpectedInvocationAndResults[4]/results/results/org.easymock.internal.Result/value/val_-value/java.util.Collections_-SynchronizedCollection/default/mutex
-------------------------------
message : Could not call java.util.Collections$SynchronizedList.writeObject()
cause-exception : com.thoughtworks.xstream.core.AbstractReferenceMarshaller$ReferencedImplicitElementException
cause-message : Cannot reference implicit element
-------------------------------
message : Could not call java.util.concurrent.ConcurrentHashMap.writeObject()
cause-exception : com.thoughtworks.xstream.converters.ConversionException
cause-message : Could not call java.util.Collections$SynchronizedList.writeObject() : Cannot reference implicit element
---- Debugging information ----
implicit-element : [EasyMock for class com.pyramid.qls.computeinvoker.Task]
referencing-element : /batchProgressMetrics/batchProgressUpdater/context/servletContext/context/attributes/java.util.concurrent.ConcurrentHashMap/org.springframework.web.context.support.XmlWebApplicationContext/servletConfig/config/parent/lifecycle/listeners/org.apache.catalina.core.StandardHost_-MemoryLeakTrackingListener/outer-class/listeners/org.apache.catalina.connector.MapperListener/connector/service/connectors/org.apache.catalina.connector.Connector[2]/container/thread/group/threads/java.lang.Thread/threadLocals/table/java.lang.ThreadLocal_-ThreadLocalMap_-Entry[11]/value/org.easymock.classextension.internal.ClassProxyFactory_-1/val_-handler/delegate/control/state/behavior/behaviorLists/org.easymock.internal.UnorderedBehavior/results/org.easymock.internal.ExpectedInvocationAndResults[4]/results/results/org.easymock.internal.Result/value/val_-value/java.util.Collections_-SynchronizedCollection/default/mutex
-------------------------------
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:659)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
我在 Windows 上使用最新的 spring 3.0.5.RELEASE jar。除此之外,我还使用 xstream-1.3.1 和 xpp4-1.1.4。你能帮我一下吗?另外,如果这是 Spring 的限制,那么您能否指导我完成一些示例,在这些示例中我可以找到运行中的宁静 Web 服务。我看过使用 jaxb 的示例,但问题是它不支持接口。以下是我的 servlet xml 中的异常
这就是我在控制器中的内容
@RequestMapping(value = "/clientMetrics/{clientId}", method = RequestMethod.GET)
public ModelAndView getBatchProgressMetrics(@PathVariable String clientId) {
List<BatchProgressMetrics> batchProgressMetricsList = null;
batchProgressMetricsList = batchProgressReporter.getBatchProgressMetricsForClient(clientId);
ModelAndView mav = new ModelAndView("QPRXmlView", BindingResult.MODEL_KEY_PREFIX + "batchProgressMetrics", batchProgressMetricsList.get(0));
return mav;
}
And on BatchProgressMetrics (is an interface) object i do have annotation:
@XStreamAlias("batchProgressMetrics")
public class BatchProgressMetricsImpl implements BatchProgressMetrics
I am beginner to webservice. I was trying to write Restful webservice and i followed
http://www.stupidjavatricks.com/?p=54. This example works for me. But when i tried to implement the same on my code base it failed with below exception:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.oxm.MarshallingFailureException: XStream marshalling exception; nested exception is com.thoughtworks.xstream.converters.ConversionException: Could not call java.util.concurrent.ConcurrentHashMap.writeObject() : Could not call java.util.Collections$SynchronizedList.writeObject() : Cannot reference implicit element
---- Debugging information ----
implicit-element : [EasyMock for class com.pyramid.qls.computeinvoker.Task]
referencing-element : /batchProgressMetrics/batchProgressUpdater/context/servletContext/context/attributes/java.util.concurrent.ConcurrentHashMap/org.springframework.web.context.support.XmlWebApplicationContext/servletConfig/config/parent/lifecycle/listeners/org.apache.catalina.core.StandardHost_-MemoryLeakTrackingListener/outer-class/listeners/org.apache.catalina.connector.MapperListener/connector/service/connectors/org.apache.catalina.connector.Connector[2]/container/thread/group/threads/java.lang.Thread/threadLocals/table/java.lang.ThreadLocal_-ThreadLocalMap_-Entry[11]/value/org.easymock.classextension.internal.ClassProxyFactory_-1/val_-handler/delegate/control/state/behavior/behaviorLists/org.easymock.internal.UnorderedBehavior/results/org.easymock.internal.ExpectedInvocationAndResults[4]/results/results/org.easymock.internal.Result/value/val_-value/java.util.Collections_-SynchronizedCollection/default/mutex
-------------------------------
message : Could not call java.util.Collections$SynchronizedList.writeObject()
cause-exception : com.thoughtworks.xstream.core.AbstractReferenceMarshaller$ReferencedImplicitElementException
cause-message : Cannot reference implicit element
-------------------------------
message : Could not call java.util.concurrent.ConcurrentHashMap.writeObject()
cause-exception : com.thoughtworks.xstream.converters.ConversionException
cause-message : Could not call java.util.Collections$SynchronizedList.writeObject() : Cannot reference implicit element
---- Debugging information ----
implicit-element : [EasyMock for class com.pyramid.qls.computeinvoker.Task]
referencing-element : /batchProgressMetrics/batchProgressUpdater/context/servletContext/context/attributes/java.util.concurrent.ConcurrentHashMap/org.springframework.web.context.support.XmlWebApplicationContext/servletConfig/config/parent/lifecycle/listeners/org.apache.catalina.core.StandardHost_-MemoryLeakTrackingListener/outer-class/listeners/org.apache.catalina.connector.MapperListener/connector/service/connectors/org.apache.catalina.connector.Connector[2]/container/thread/group/threads/java.lang.Thread/threadLocals/table/java.lang.ThreadLocal_-ThreadLocalMap_-Entry[11]/value/org.easymock.classextension.internal.ClassProxyFactory_-1/val_-handler/delegate/control/state/behavior/behaviorLists/org.easymock.internal.UnorderedBehavior/results/org.easymock.internal.ExpectedInvocationAndResults[4]/results/results/org.easymock.internal.Result/value/val_-value/java.util.Collections_-SynchronizedCollection/default/mutex
-------------------------------
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:659)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
I am using latest spring 3.0.5.RELEASE jars on windows. In addition to it i am using xstream-1.3.1 and xpp4-1.1.4. Can you please help me. Also if this is the limitation of Spring then can you please guide me through some of the examples where i can find restful webservice in action. I have seen examples using jaxb but there the issue was it doesn't support interface. Following is exception from my servlet xml
This is what i have in controller
@RequestMapping(value = "/clientMetrics/{clientId}", method = RequestMethod.GET)
public ModelAndView getBatchProgressMetrics(@PathVariable String clientId) {
List<BatchProgressMetrics> batchProgressMetricsList = null;
batchProgressMetricsList = batchProgressReporter.getBatchProgressMetricsForClient(clientId);
ModelAndView mav = new ModelAndView("QPRXmlView", BindingResult.MODEL_KEY_PREFIX + "batchProgressMetrics", batchProgressMetricsList.get(0));
return mav;
}
And on BatchProgressMetrics (is an interface) object i do have annotation:
@XStreamAlias("batchProgressMetrics")
public class BatchProgressMetricsImpl implements BatchProgressMetrics
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来像是 XStream 问题而不是 spring 问题。需要注意以下几点:
detectannotations
设置为 true (如示例中所示)还要升级 XStream(如果没有运行最新版本)
如果问题确实是同步集合不起作用(我对此表示怀疑),那么在从方法返回之前,您可以从 -
new ArrayList(syncedList) 创建一个新集合)
。通过线路发送时无需同步结果。This seems like an XStream problem rather than spring. A few things to look at:
detectannotations
is set to true (as in the example)Also upgrade XStream (if not running the latest vesrsion)
If the problem is really that synchronized collections don't work (which I doubt), then before returning from your methods you can create a new collection from -
new ArrayList(syncedList)
. The result needs not be synchronized when sending over the wire.