gpt4 book ai didi

java - 如何使用serialize Collections.synchronizedList和co

转载 作者:行者123 更新时间:2023-12-01 05:51:14 25 4
gpt4 key购买 nike

我是网络服务的初学者。我试图编写 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 的限制,那么您能否指导我完成一些示例,在这些示例中我可以找到运行中的 Restful Web 服务。我看过使用 jaxb 的示例,但问题是它不支持接口(interface)。以下是我的 servlet xml 中的异常






这就是我在 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

最佳答案

这看起来像是 XStream 问题而不是 spring 问题。需要注意以下几点:

  • 确保 detectannotations 设置为 true(如示例所示)
  • 确保您作为响应发送的对象已使用 XStream 注释进行注释。

同时升级 XStream(如果未运行最新版本)

如果问题确实是同步集合不起作用(我对此表示怀疑),那么在从方法返回之前,您可以从 - new ArrayList(syncedList) 创建一个新集合。通过网络发送时,结果不需要同步。

关于java - 如何使用serialize Collections.synchronizedList和co,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4634360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com