Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid public int field from JAXB serialization?

Tags:

java

xml

jaxb

How can I avoid field from being serialized? I use xml attributes. Currently field has no attribute but gets to xml...

like image 371
Stepan Yakovenko Avatar asked Dec 15 '22 19:12

Stepan Yakovenko


2 Answers

Annotate the field you want to exclude with @XmlTransient.

like image 74
dcernahoschi Avatar answered Jan 01 '23 10:01

dcernahoschi


Option #1 - Change the Accessor Type

By default a JAXB (JSR-222) implementation will treat all public fields and properties as being mapped. If you want to restrict this to just public properties then you can do the following:

@XmlAccessorType(XmlAccessType.PROPERTY)
public class Foo {

    public int bar; // Not considered mapped if access type is set to PROPERTY

}
  • http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html

Option #2 - Specify the Field is Unmapped

You can mark a field/property with @XmlTransient to prevent it from being mapped.

public class Foo {

    @XmlTransient
    public int bar; // Not considered mapped

}
  • http://blog.bdoughan.com/2012/04/jaxb-and-unmapped-properties.html
like image 35
bdoughan Avatar answered Jan 01 '23 10:01

bdoughan