JAXB 绑定 - 将 typesafeEnumBase 设置为空(将枚举转换为字符串)
JAXB bindings - set typesafeEnumBase to empty (convert enumerations to strings)
根据link
If typesafeEnumBase is set to xsd:string, it would be a global way to
specify that all simple type definitions deriving directly or
indirectly from xsd:string and having enumeration facets should be
bound by default to a typesafe enum. If typesafeEnumBase is set to an
empty string, "", no simple type definitions would ever be bound to a
typesafe enum class by default. The value of typesafeEnumBase can be
any atomic simple type definition except xsd:boolean and both binary
types.
所以我已经为我的 binding.xjb
设置了以下内容:
<jxb:globalBindings typesafeEnumBase=""/>
当 运行 和 jaxb2-maven-plugin
时,我得到以下异常:
lineNumber: 5; columnNumber: 46; cvc-minLength-valid: Value '' with length = '0' is not facet-valid with respect to minLength '1' for type '#AnonType_typesafeEnumBaseglobalBindings'.
...
lineNumber: 5; columnNumber: 46; cvc-attribute.3: The value '' of attribute 'typesafeEnumBase' on element 'jxb:globalBindings' is not valid with respect to its type, 'null'.
据我所知,我不能将空字符串 ""
设置为 typesafeEnumBase
,即使文档是这样说的。文档还提到它不能是 xsd:boolean
。
我只想将以下内容转换为 String
而不是 enum
<xs:simpleType name="phraseID">
<xs:restriction base="escapedStringUserType">
<xs:enumeration value="NOT_SPECIFIED"/>
<xs:enumeration value="X000-9999"/>
<xs:enumeration value="X000-9998"/>
</xs:restriction>
</xs:simpleType>
一个相关的 SO 问题是 here 但由于我无法设置空字符串 ""
或将 xsd:boolean
值设置为 typesafeEnumBase
none答案对我有用(都试过了)。
您可以为具有枚举限制的特定简单类型指定您不希望将其映射到 Java enum
,如本例所示:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0">
<jaxb:bindings
schemaLocation="myschema.xsd"
node="/xs:schema/xs:simpleType[@name='phraseID']">
<jaxb:typesafeEnumClass map="false"/>
</jaxb:bindings>
</jaxb:bindings>
但据我所知,没有办法为所有具有枚举限制的简单类型全局设置它(例如,你不能在 <jaxb:globalBindings>
中直接使用 <jaxb:typesafeEnumClass map="false"/>
)。
(如果有人知道怎么做,我也想知道,所以请评论或回答)。
设置以下
<jxb:globalBindings typesafeEnumMaxMembers="0"/>
不会为所有具有枚举限制的简单类型生成任何枚举,而是会将它们转换为字符串,但它会在控制台中发出警告,如下所示:
Simple type "xxx-address" was not mapped to Enum due to EnumMemberSizeCap limit. Facets count: 10, current limit: 0. You can use customization attribute "typesafeEnumMaxMembers" to extend the limit.
根据link
If typesafeEnumBase is set to xsd:string, it would be a global way to specify that all simple type definitions deriving directly or indirectly from xsd:string and having enumeration facets should be bound by default to a typesafe enum. If typesafeEnumBase is set to an empty string, "", no simple type definitions would ever be bound to a typesafe enum class by default. The value of typesafeEnumBase can be any atomic simple type definition except xsd:boolean and both binary types.
所以我已经为我的 binding.xjb
设置了以下内容:
<jxb:globalBindings typesafeEnumBase=""/>
当 运行 和 jaxb2-maven-plugin
时,我得到以下异常:
lineNumber: 5; columnNumber: 46; cvc-minLength-valid: Value '' with length = '0' is not facet-valid with respect to minLength '1' for type '#AnonType_typesafeEnumBaseglobalBindings'.
...
lineNumber: 5; columnNumber: 46; cvc-attribute.3: The value '' of attribute 'typesafeEnumBase' on element 'jxb:globalBindings' is not valid with respect to its type, 'null'.
据我所知,我不能将空字符串 ""
设置为 typesafeEnumBase
,即使文档是这样说的。文档还提到它不能是 xsd:boolean
。
我只想将以下内容转换为 String
而不是 enum
<xs:simpleType name="phraseID">
<xs:restriction base="escapedStringUserType">
<xs:enumeration value="NOT_SPECIFIED"/>
<xs:enumeration value="X000-9999"/>
<xs:enumeration value="X000-9998"/>
</xs:restriction>
</xs:simpleType>
一个相关的 SO 问题是 here 但由于我无法设置空字符串 ""
或将 xsd:boolean
值设置为 typesafeEnumBase
none答案对我有用(都试过了)。
您可以为具有枚举限制的特定简单类型指定您不希望将其映射到 Java enum
,如本例所示:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0">
<jaxb:bindings
schemaLocation="myschema.xsd"
node="/xs:schema/xs:simpleType[@name='phraseID']">
<jaxb:typesafeEnumClass map="false"/>
</jaxb:bindings>
</jaxb:bindings>
但据我所知,没有办法为所有具有枚举限制的简单类型全局设置它(例如,你不能在 <jaxb:globalBindings>
中直接使用 <jaxb:typesafeEnumClass map="false"/>
)。
(如果有人知道怎么做,我也想知道,所以请评论或回答)。
设置以下
<jxb:globalBindings typesafeEnumMaxMembers="0"/>
不会为所有具有枚举限制的简单类型生成任何枚举,而是会将它们转换为字符串,但它会在控制台中发出警告,如下所示:
Simple type "xxx-address" was not mapped to Enum due to EnumMemberSizeCap limit. Facets count: 10, current limit: 0. You can use customization attribute "typesafeEnumMaxMembers" to extend the limit.