Search code examples
javaxmlxml-serializationjaxb2

Preventing a class member conversion to JAXB XML


Here is my JAXB class,

@XmlRootElement
public class Status {

    private int code;
    private String message;

    public Status() {
    }

    public Status(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

I dont want the 'code' to be marshalled to XML. Here are the things I tried, but it always marshalled to XML.

  • Annotate @XMLElement to only getMessage() method
  • Make 'code' as transient

No hopes yet. The XMLAccessorType.NONE can be applied to class level. Not in element level. Please help.


Solution

  • Add @XmlTransient annotation before the getter methods of the attributes you don't want to be marshalled.

    Ex:

    @XmlTransient
    public int getCode() {
            return code;
        }