java XStream:如何在jdks之间进行2个进程集合?

时间:2014-06-11 19:59:24

标签: java collections hashmap xstream

我在ibm jdk(IBM-Java60-amd64)中生成了一个httpSession,并使用XStream解析为xml。 我想在其他jdk(orecle-jdk1.6.0-x86)上重新创建此会话映射,但在反序列化时会发生错误。

我的Xstream对象:

public static XStream getXStreamConfigured( ){

 final XStream xstream = new XStream(){

  @Override
  protected MapperWrapper wrapMapper( final MapperWrapper next ){

    return new MapperWrapper( next )
    {

      @Override
      public boolean shouldSerializeMember(
           final Class definedIn,
           final String fieldName ){

         if ( definedIn == Object.class ){
            return false;
         }

         return super.shouldSerializeMember( definedIn, fieldName );
      }

    };      
  }

};

 xstream.autodetectAnnotations( true );

 final String dateFormatXmlPattern = FundXmlHandler.DATE_FORMAT_VIEW_AND_XML_FORMAT;
 final DateConverter dateConverter = new XStreamConverterDate( dateFormatXmlPattern );
 xstream.registerConverter( dateConverter );

 return xstream;

}

使用oracle jdk,在这个输出xml中生成的一些标签就像:

<java.util.TreeMap_-KeySet>
  <m class="tree-map"/>
</java.util.TreeMap_-KeySet>

使用ibm jdk,在这个输出xml中生成的一些标签就像:

<java.util.TreeMap_-1>
    <outer_class/>
</java.util.TreeMap_-1>

为什么它会为TreeMaps生成这个? 为什么我无法在oracle jdk中加载由ibm jdk生成的xml? 如何写入一个在jdk中生成相同输出的转换器?

我的筹码:

---- Debugging information ----
message             : java.util.TreeMap$1
cause-exception     : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message       : java.util.TreeMap$1
class               : java.util.HashMap
required-type       : java.util.HashMap
converter-type      : com.thoughtworks.xstream.converters.collections.MapConverter
path                : /map/entry[92]/java.util.TreeMap$1
line number         : 161079
version             : null
-------------------------------
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1052)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1036)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:912)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:903)
    at br.com.mindsatwork.vinci.rwf.util.UtilitiesRwf.loadSavedSessionObject(UtilitiesRwf.java:3222)
    at br.com.mindsatwork.vinci.rwf.util.UtilitiesRwf.getFundDataFromSavedSession(UtilitiesRwf.java:1912)
    at br.com.mindsatwork.vinci.rwf.web.action.ActionHandleSavedSession.getFundData(ActionHandleSavedSession.java:86)
    at br.com.mindsatwork.vinci.rwf.web.action.AbstractActionWithFundDataUpload.executeIfLogged(AbstractActionWithFundDataUpload.java:88)
    at br.com.mindsatwork.vinci.rwf.web.action.ActionHandleSavedSession.executeIfLogged(ActionHandleSavedSession.java:64)
    at br.com.mindsatwork.vinci.rwf.web.action.AbstractActionWithLogin.executeAction(AbstractActionWithLogin.java:80)
    at br.com.mindsatwork.vinci.rwf.web.action.AbstractVinciAction.executeAction(AbstractVinciAction.java:148)
    at br.com.utilities.servlet.struts.action.AbstractStrutsActionWithDynaForm.executeAction(AbstractStrutsActionWithDynaForm.java:75)
    at br.com.utilities.servlet.struts.action.AbstractStrutsAction.execute(AbstractStrutsAction.java:57)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:476)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:239)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1334)
    at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:426)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at br.com.utilities.servlet.struts.AbstractStrutsAppServer.service(AbstractStrutsAppServer.java:344)
    at br.com.mindsatwork.vinci.rwf.web.AppServer.service(AppServer.java:303)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:576)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:306)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:322)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1732)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: com.thoughtworks.xstream.mapper.CannotResolveClassException: java.util.TreeMap$1
    at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:56)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:55)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:79)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:74)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:45)
    at com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29)
    at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readItem(AbstractCollectionConverter.java:70)
    at com.thoughtworks.xstream.converters.collections.MapConverter.putCurrentEntryIntoMap(MapConverter.java:89)
    at com.thoughtworks.xstream.converters.collections.MapConverter.populateMap(MapConverter.java:77)
    at com.thoughtworks.xstream.converters.collections.MapConverter.populateMap(MapConverter.java:71)
    at com.thoughtworks.xstream.converters.collections.MapConverter.unmarshal(MapConverter.java:66)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    ... 42 more

如何将通用集合类型编写到此输出xml中,可以将其组合到不同的jdks中?

链接http://x-stream.github.io/faq.html表明XStream有两种操作模式,Pure Java和Enhanced(默认)。据说Pure Java可以与各种JVM一起使用。 我尝试使用Pure Java实例化使用PureJavaReflectionProvider的XStream。

在同一个JVM中发生以下错误:

 com.thoughtworks.xstream.converters.ConversionException: Cannot construct java.util.TreeMap$KeySet as it does not have a no-args constructor : Cannot construct java.util.TreeMap$KeySet as it does not have a no-args constructor
---- Debugging information ----
message             : Cannot construct java.util.TreeMap$KeySet as it does not have a no-args constructor
cause-exception     : com.thoughtworks.xstream.converters.reflection.ObjectAccessException
cause-message       : Cannot construct java.util.TreeMap$KeySet as it does not have a no-args constructor
class               : java.util.TreeMap$KeySet
required-type       : java.util.TreeMap$KeySet
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /map/entry[93]/java.util.TreeMap$KeySet
line number         : 36154
class[1]            : java.util.HashMap
converter-type[1]   : com.thoughtworks.xstream.converters.collections.MapConverter
version             : null
-------------------------------

2 个答案:

答案 0 :(得分:0)

首先回答你的第一个问题:

  

为什么它会为TreeMaps生成这个?

元素本地名称为java.util.TreeMap_-1,因为XStream试图序列化java.util.TreeMap的匿名内部类。

至于第二个问题:

  

如何将通用集合类型编写到此输出xml中,可以将其组合到不同的jdks中?

您需要发布更完整的代码来演示问题。请参阅SSCCE.org以了解所需的代码。就目前而言,您还没有为其他人提供足够的例子来重现问题。

我会根据你提供的内容对这个问题进行一些有根据的猜测。

开箱即用,XStream提供a number of converters,包括TreeMapConverter java.util.TreeMap。当我在我的环境中针对TreeMap运行XStream的序列化时,我看不到KeySet内部类,也没有看到序列化的匿名内部类:

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.mapper.MapperWrapper;
import java.util.Date;
import java.util.TreeMap;

public class App {

    public static XStream getXStreamConfigured() {

        final XStream xstream = new XStream() {

            @Override
            protected MapperWrapper wrapMapper(final MapperWrapper next) {

                return new MapperWrapper(next) {

                    @Override
                    public boolean shouldSerializeMember(
                            final Class definedIn,
                            final String fieldName) {

                        if (definedIn == Object.class) {
                            return false;
                        }

                        return super.shouldSerializeMember(definedIn, fieldName);
                    }

                };
            }

        };

        xstream.autodetectAnnotations(true);

        return xstream;
    }

    public static void main(String[] args) {
        XStream xs = getXStreamConfigured();

        xs.toXML(new ExampleContainer().fill(), System.out);
    }

    public static class ExampleContainer {

        private TreeMap<String, Date> map = new TreeMap<>();

        public ExampleContainer() {
        }

        public ExampleContainer fill() {

            map.put("a", new Date());
            map.put("b", new Date());
            map.put("c", new Date());
            map.put("d", new Date());
            return this;
        }
    }
}

该代码给出了以下输出:

<App_-ExampleContainer>
  <map>
    <entry>
      <string>a</string>
      <date>2014-06-21 22:40:53.598 UTC</date>
    </entry>
    <entry>
      <string>b</string>
      <date>2014-06-21 22:40:53.598 UTC</date>
    </entry>
    <entry>
      <string>c</string>
      <date>2014-06-21 22:40:53.598 UTC</date>
    </entry>
    <entry>
      <string>d</string>
      <date>2014-06-21 22:40:53.598 UTC</date>
    </entry>
  </map>
</App_-ExampleContainer>

这似乎告诉我XStream正在使用它的默认转换器,它使用反射来检查类TreeMap的成员。导致此问题的一些可能原因包括错误地修改MapperWrapper或注册错误的Converter

为了更好的分析,请提供SSCCE来证明您的问题。

答案 1 :(得分:0)

我找到了解决问题的方法。

首先,我必须删除所有匿名内部类,并将该代码片段放在公共类中。 例如,我将每个单独的匿名Comparator移动到特定的Comparator类中。这也改善了设计。

我需要编写MapConverter,CollectionConverter和ComparatorConverter来适当地序列化和组装对象。 这个类扩展了XStream API提供的MapConverter,CollectionConverter和JavaBeanConverter。

我创建了一个静态String ForbiddenClassesList,它包含确定的jdk实现的所有特定类名。例如,我的列表有:

Collections.EMPTY_LIST.getClass()

Collections.singletonList( null ).getClass()

在这些情况下,我只是忽略了,并在marshal方法执行中序列化了一个非常空的集合 - new ArrayList()

我还根据以下列表测试了源类名:

{"java.util.TreeMap$KeySet","java.util.TreeMap$1"}

在我的CollectionConverter中,不转换对象并将对象重定向到右转换器,例如我的MapConverter。

之后,一切正常! =)

避免使用所有匿名类,并在这种情况下实现特定的类。