[英]Is it possible using Jersey/JAX-RS annotations to skip a class member when marshalling to XML/JSON?
Pretty straightforward question. 非常直截了当的问题。 I am using Jersey to build a REST system.
我正在使用Jersey构建一个REST系统。 If I have a class with a value that I need to use during processing but don't want sent as part of the XML or JSON output when the class is marshaled, is there a way to ignore it?
如果我有一个具有值的类,我需要在处理期间使用但不希望在类被封送时作为XML或JSON输出的一部分发送,有没有办法忽略它? Something like:
就像是:
@XmlRootElement(name="example")
class Example {
private int a;
private String b;
private Object c;
@XmlElement(ignore=true)
public int getA() { return a; }
@XmlElement
public String getB() { return b; }
@Ignore
public Object getC() { return c; }
... //setters, constructors, etc.
}
I would hope that something like the ignore=true
over getA()
or the @Ignore
over getC()
would work, but i can find no documentation. 我希望类似的
ignore=true
在getA()
或@Ignore
超过getC()
的工作,但我找不到任何文档。
There are couple options depending on how many fields/properties you want to be ignored. 根据您想要忽略的字段/属性数量,有几个选项。
Option #1 - @XmlTransient
选项#1 -
@XmlTransient
If you want less than half of the properties to be ignored then I would recommend annotating them with @XmlTransient
. 如果你想要忽略不到一半的属性,那么我建议用
@XmlTransient
注释它们。 This will exclude them from the XML mapping. 这将把它们从XML映射中排除。
@XmlRootElement
class Example {
private int a;
private String b;
private Object c;
@XmlTransient
public int getA() { return a; } // UNMAPPED
public String getB() { return b; } // MAPPED
@XmlTransient
public Object getC() { return c; } // UNMAPPED
... //setters, constructors, etc.
}
Option #2 - @XmlAccessorType(XmlAccessType.NONE)
选项#2 -
@XmlAccessorType(XmlAccessType.NONE)
If you want more than half of the properties ignored I would recommend using the @XmlAccessorType
annotation at the type level to set XmlAccessType.NONE
. 如果您希望忽略超过一半的属性,我建议在类型级别使用
@XmlAccessorType
批注来设置XmlAccessType.NONE
。 This will cause only annotated properties to be mapped to XML. 这将导致仅将带注释的属性映射到XML。
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
class Example {
private int a;
private String b;
private Object c;
public int getA() { return a; } // UNMAPPED
@XmlElement
public String getB() { return b; } // MAPPED
public Object getC() { return c; } // UNMAPPED
... //setters, constructors, etc.
}
For More Information 欲获得更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.