Java – Byte array is not working in JAXB classes

javajaxb

I am trying to use byte array like this (JAXB class). However, I am getting all 0s in the msg field even though I pass valid characters. The "id" and "myid" fields are parsed successfully and it is failing for the byte array field.

@XmlRootElement(name = "testMessage")
@XmlAccessorType(XmlAccessType.FIELD)
public class TestMessage
{
    @XmlAttribute
    private Integer id;

    @XmlElement(name = "myid")
    private Long myid;

    @XmlElement(name = "msg")
    private byte[] msg;
}

Best Solution

Using JAXB of Java 1.6.0_23 i get the following xml file for a TestMessage instance:

TestMessage testMessage = new TestMessage();
testMessage.id = 1;
testMessage.myid = 2l;
testMessage.msg = "Test12345678".getBytes();

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<testMessage id="1">
    <myid>2</myid>
    <msg>VGVzdDEyMzQ1Njc4</msg>
</testMessage>

If you unmarshall this xml content you should get back the TestMessage instance including the msg byte array (which is base64 encoded in the xml file).