13
4

5 回答 5

20

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

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

 xstream.aliasSystemAttribute(null, "class");
于 2011-04-24T16:01:57.297 回答
14

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

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

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

有关详细信息,请参阅http://markmail.org/message/gds63p3dnhpy3ef2 。

于 2010-02-23T23:23:12.787 回答
2

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

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();
}
于 2010-04-20T13:25:34.460 回答
2

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

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

.

于 2015-05-06T00:41:14.693 回答
1

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

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

有关更深入的报道,请参阅http://x-stream.github.io/alias-tutorial.html 。再次,粘贴一些示例输出。

于 2010-01-06T19:41:23.760 回答