Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Byte array is not working in JAXB classes

Tags:

java

jaxb

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;
}
like image 871
user243655 Avatar asked Oct 14 '22 18:10

user243655


1 Answers

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).

like image 144
Robert Avatar answered Oct 29 '22 00:10

Robert