如何使用serialize Collections.synchronizedList和co

时间:2011-01-08 14:36:41

标签: java spring spring-mvc xstream

我是webservice的初学者。我试图写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上使用最新的春季3.0.5.RELEASE罐子。除此之外,我使用的是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  

1 个答案:

答案 0 :(得分:0)

这似乎是一个XStream问题,而不是春天。有几点需要注意:

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

同时升级XStream(如果没有运行最新的版本)

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