You can use xml adapters for your byte array xml element. As you now, every element get marshalling/unmarshalling and adapters are use for situations such as converting date time with specified format, type convertions etc. while marshalling/unmarshalling.
HexBinaryAdapter class is one of those adapters belongs to javax.xml.bind.annotation.adapters so you can use it.
public class TestMessage {
@XmlAttribute
private Integer id;
@XmlElement(name = "myid")
private Long myid;
@XmlJavaTypeAdapter(HexBinaryAdapter.class)
@XmlElement(name = "msg")
private byte[] msg;
}
Yet, if you prefer a custom convertion, you can create your own adapter for converting bytes for a specified format such as base64 etc.
To do that you must write your own unmarshalling/marshalling methods,
public final class MyAdapter extends XmlAdapter<String, byte[]> {
public byte[] unmarshal(String s) {
if (s == null)
return null;
return decode()); // your way to decode.
}
public String marshal(byte[] bytes) {
if (bytes == null)
return null;
return encode(); //your way to encode
}
}
then you give your marshaller/unmarshaller in @XmlJavaTypeAdapter anotation ;
public class TestMessage {
@XmlAttribute
private Integer id;
@XmlElement(name = "myid")
private Long myid;
@XmlJavaTypeAdapter(MyAdapter.class)
@XmlElement(name = "msg")
private byte[] msg;
}