用于base64Binary的XmlAdapter导致String [英] XmlAdapter for base64Binary results in String
本文介绍了用于base64Binary的XmlAdapter导致String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含以下内容的XSD文件:
I've an XSD file containing this:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc"
elementFormDefault="qualified"
targetNamespace="http://example.org/">
<xsd:complexType name="Certificate">
<xsd:sequence>
<xsd:element name="certificate" type="xsd:base64Binary">
<xsd:annotation>
<xsd:appinfo>
<xjc:javaType name="java.security.cert.X509Certificate" adapter="adapters.X509CertificateAdapter" />
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
当我使用xjc生成java代码时,它会生成:
and when I generate java code with xjc, it produces this:
public class Certificate {
@XmlElement(required = true, type = String.class)
@XmlJavaTypeAdapter(X509CertificateAdapter.class)
@XmlSchemaType(name = "base64Binary")
protected X509Certificate certificate;
....
}
并且适配器工作正常很好。
and the adapter works fine.
我的问题是为什么 @XmlElement(required = true,type = String.class)
?为什么 type = String.class
而不是 byte []
?
My question is why @XmlElement(required = true, type = String.class)
? Why type = String.class
and not byte[]
?
推荐答案
对于以下内容:
@XmlElement(required = true, type = String.class)
@XmlJavaTypeAdapter(X509CertificateAdapter.class)
@XmlSchemaType(name = "base64Binary")
protected X509Certificate certificate;
有几点需要注意:
-
@XmlElement
上的类型
设置是多余的,因为X509CertificateAdapter
已经指定用于编组&解组它将X508Certificate
转换为String
。 -
X509CertificateAdapter
转换为String
而不是byte []
最有可能出于性能目的,因为转换为byte []
会导致JAXB额外转换为String
在XML中正确呈现它。 -
@XmlSchemaType
注释用于确保XML Schema时生成的节点类型显示为base64Binary
而不是string
。
- The
type
setting on@XmlElement
is redundant since theX509CertificateAdapter
already specifies that for the purposes of marshalling & unmarshalling it is going to convertX508Certificate
to/from aString
. - The
X509CertificateAdapter
converts to/from aString
instead of abyte[]
most likely for the purposes of performance, as a conversion to abyte[]
would cause JAXB to do an additional conversion toString
to render it correctly in the XML. - The
@XmlSchemaType
annotation is used to ensure that when an XML Schema is generated that the node type shows up asbase64Binary
and notstring
.
这篇关于用于base64Binary的XmlAdapter导致String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文