【问题标题】:Xstream: removing class attributeXstream:删除类属性
【发布时间】:2010-01-05 17:59:32
【问题描述】:

如何删除 Xstream 中的 class=”Something ” 属性。

我使用带有注释的 Xstream

【问题讨论】:

标签: java attributes annotations xstream


【解决方案1】:

我看了它的代码,发现如果你的类不是mapper.defaultImplementationOf(fieldType),它会为你添加默认的类属性,除非类属性名称为空;

所以,设置这个可以去掉 class=”Something” 属性

 xstream.aliasSystemAttribute(null, "class");

【讨论】:

  • 是否有可用的 spring oxm 替代品?
【解决方案2】:

确实,这个问题并没有像它应该的那样清楚地表达出来。我的猜测是您使用的是非标准集合或使用 XStream 需要为其存储实际类的接口类型的字段。

在第二种情况下,您可以只使用别名:

xstream.alias("field name", Interface.class, ActualClassToUse.class);

更多详情请见http://markmail.org/message/gds63p3dnhpy3ef2

【讨论】:

  • 我遇到了类似的问题,结果证明是这个问题。我担心我必须使用别名 javacode 而不是注释,但结果证明它们可以很好地互补,因为我可以确定在运行时使用的实际类。
  • 是否有可用的 spring oxm 替代品?
【解决方案3】:

使用这种东西来完全删除类属性,而不是用其他东西给它起别名:

private String generateResponse(final XStream xStream)
{
    StringWriter writer = new StringWriter();
    xStream.marshal(this, new PrettyPrintWriter(writer) {
        @Override
        public void addAttribute(final String key, final String value)
        {
            if (!key.equals("class"))
            {
                super.addAttribute(key, value);
            }
        }
    });
    return writer.toString();
}

【讨论】:

    【解决方案4】:

    至少在不明显应该使用哪个类时显示此属性。接口的使用就是一个例子。 在这种情况下您可以尝试:

    xStream.addDefaultImplementation(YourDefaultImplementation.class, YourInterface.class);
    

    .

    【讨论】:

      【解决方案5】:

      你能给出一些示例输出吗?我认为这通常在使用 Collections 时发生。没有看到输出,我最好的猜测是你需要注册别名:

      xstream.alias("blog", Blog.class);
      

      请参阅http://x-stream.github.io/alias-tutorial.html 了解更深入的报道。同样,粘贴一些示例输出。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-16
        • 2011-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-10
        • 1970-01-01
        • 2013-08-09
        相关资源
        最近更新 更多