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.
Annotations in the JAX-RS API are used to provide meta-data around the web resource. A typical example is to use the @GET annotation with the @Path annotation to identify the method that should handle a GET request to the specified URI in the @Path annotation.
Jersey is a Java library for both serving and calling REST (or mainly HTTP, since not everything is REST) APIs. It's build on top of Java EE specifications, so it can be used on any server that implements these specifications, e.g. Tomcat. In your web. xml file you can define multiple servlets.
Jersey is Sun's production quality reference implementation for JSR 311: JAX-RS: The Java API for RESTful Web Services. Jersey implements support for the annotations defined in JSR-311, making it easy for developers to build RESTful web services with Java and the Java JVM.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With