用于base64Binary的XmlAdapter导致String [英] XmlAdapter for base64Binary results in String

查看:101
本文介绍了用于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;

有几点需要注意:


  1. @XmlElement 上的类型设置是多余的,因为 X509CertificateAdapter 已经指定用于编组&解组它将 X508Certificate 转换为 String

  2. X509CertificateAdapter 转换为 String 而不是 byte [] 最有可能出于性能目的,因为转换为 byte [] 会导致JAXB额外转换为 String 在XML中正确呈现它。

  3. @XmlSchemaType 注释用于确保XML Schema时生成的节点类型显示为 base64Binary 而不是 string

  1. The type setting on @XmlElement is redundant since the X509CertificateAdapter already specifies that for the purposes of marshalling & unmarshalling it is going to convert X508Certificate to/from a String.
  2. The X509CertificateAdapter converts to/from a String instead of a byte[] most likely for the purposes of performance, as a conversion to a byte[] would cause JAXB to do an additional conversion to String to render it correctly in the XML.
  3. The @XmlSchemaType annotation is used to ensure that when an XML Schema is generated that the node type shows up as base64Binary and not string.

这篇关于用于base64Binary的XmlAdapter导致String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆