JaxB - 设置HexBinary值

时间:2011-10-04 17:22:24

标签: java jaxb

我试图通过使用spring和jaxb来调用简单的XML over HTTP服务。该服务有一个请求属性为 < xsd:attribute name =“version”type =“xsd:hexBinary”use =“required”/>

JAXB生成相应的java包装器对象

/**
 * 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);
}

这里,虽然服务将其定义为hexbinary,但我的版本实际上是内部版本。我没有控制服务实现来将类型从hexbinary转换为unsignedint。

在向服务发出请求时,我喜欢将版本号设置为myBean.setVersion(12作为字节),其中12只是一个长数字。如何将long转换为byte []以便能够调用setVersion();

谢谢, 希瓦。

1 个答案:

答案 0 :(得分:2)

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;
    }
}

byte[]的默认绑定为xsd:base64Binary

您可以像

一样进行更改
@XmlElement
@XmlSchemaType(name="hexBinary")
public byte[] getVersion() {
    return version;
}