JaxB - Setting HexBinary values
I am trying to call a simple XML over HTTP service by using spring and jaxb. The service has one of its request attribtues as <xsd:attribute name="versi开发者_运维技巧on" type="xsd:hexBinary" use="required"/>
JAXB generates the corresponding java wrapper object as
/**
* Gets the value of the version property.
*
* @return
* possible object is
* {@link String }
*
*/
public byte[] getVersion() {
return version;
}
/**
* Sets the value of the version property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setVersion(byte[] value) {
this.version = ((byte[]) value);
}
Here, my version is actually a long internally although the service defined it as hexbinary. I don't have control over the service implementation to convert the type from hexbinary to unsignedint.
While making a request to the service, I like to set the version number as myBean.setVersion(12 as bytes) where 12 is just a long number. How do I convert long to byte[] to be able to call setVersion();
Thanks, Siva.
byte[] longToBytes(long value) {
final byte[] bytes = new byte[8];
for (int i = bytes.length - 1; i >= 0; i--) {
bytes[i] = (byte)(value & 0xFF);
value >>>= 8;
}
}
The default binding for byte[]
is xsd:base64Binary
.
You can change it like
@XmlElement
@XmlSchemaType(name="hexBinary")
public byte[] getVersion() {
return version;
}
精彩评论