将枚举转换为字符串

将枚举转换为字符串

本文介绍了JAXB绑定 - 将typesafeEnumBase设置为空(将枚举转换为字符串)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据

所以我设置了我的 binding.xjb 以下内容:

So I have set to my binding.xjb the following:

<jxb:globalBindings typesafeEnumBase=""/>

以及运行 jaxb2-maven-plugin时我得到以下异常:

and when running the jaxb2-maven-plugin I get the following exception:

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

As far as I understand I cannot set the empty string "" to typesafeEnumBase even though the documentation says so. Documentation also mentions that it cannot be xsd:boolean.

我想要的只是将以下内容转换为 String 而不是 enum

All I want is to convert the following to String instead of 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问题是但由于我无法设置空字符串或设置 xsd:boolean 的值为 typesafeEnumBase 这些答案都不适合我(两者都试过)。

A relevant SO question is here but since I cannot set the empty string "" or set the xsd:boolean value to typesafeEnumBase none of the answers work for me (tried both).

推荐答案

设置以下内容

<jxb:globalBindings typesafeEnumMaxMembers="0"/>

不会为具有枚举限制的所有简单类型生成任何枚举,而是将它们转换为字符串,但是在控制台中吐出警告,如下所示:

will not generate any enums for all simple types with enumeration restrictions instead will convert them to strings, however it spits out warnings in the console like the following:

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.

这篇关于JAXB绑定 - 将typesafeEnumBase设置为空(将枚举转换为字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 20:17