Is it possible using Jersey/JAX-RS annotations to skip a class member when marshalling to XML/JSON?
jax-rs, jaxb, jersey
Solution
There are couple options depending on how many fields/properties you want to be ignored.
Option #1 - `@XmlTransient`
If you want less than half of the properties to be ignored then I would recommend annotating them with `@XmlTransient`. This will exclude them from the XML mapping.
@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)`
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`. This will cause only annotated properties to be mapped to 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
- http://blog.bdoughan.com/2012/04/jaxb-and-unmapped-properties.html
Problem
Pretty straightforward question. I am using Jersey to build a REST system. 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? 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.