java - 关于 Jaxb 2.x SchemaGen 的问题

标签 java xsd jaxb2

我正在尝试使用 jaxb 从我现有的 POJO 类生成模式,到目前为止它工作正常,现在我有一个要求,我需要声明属性类型是我的 XSD,但属性值应该是预定义值之一。 下面是我类(class)的代码快照

private String destinationID;
private String contactNo;
private String type;
@XmlAttribute
private String name;

我的要求是名称应包含任何与此类似的预定义值

<xsd:attribute name="type"
        type="simpleType.Generic.ProductReferenceType" use="required" />
<xsd:simpleType name="simpleType.Generic.ProductReferenceType">
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="OFFER" />
        <xsd:enumeration value="SELLER" />
        <xsd:enumeration value="DEFINITION" />
    </xsd:restriction>
</xsd:simpleType>

我无法找出我需要在类里面做什么才能实现这种情况

提前致谢

最佳答案

您可以像这样定义枚举:

@XmlType(name="simpleType.Generic.ProductReferenceType")
public enum ProductReferenceType { 
    OFFER,
    SELLER,
    DEFINITION
}

然后简单地在你的类中使用它:

@XmlAttribute
public ProductReferenceType type;

这将生成 XSD,如下所示:

  <xs:simpleType name="simpleType.Generic.ProductReferenceType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="OFFER"/>
      <xs:enumeration value="SELLER"/>
      <xs:enumeration value="DEFINITION"/>
    </xs:restriction>
  </xs:simpleType>

    <xs:attribute name="type" type="simpleType.Generic.ProductReferenceType"/>

祝您的项目顺利!

关于java - 关于 Jaxb 2.x SchemaGen 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4354655/

相关文章:

MS 2007 应用程序中的 Java 屏幕捕获和粘贴问题

java - Tomcat 无法使用 cargo maven 启动组件

java - Spring JDBC如何在不使用JTA的情况下使用事务管理器实现多数据源

c# - 引用通用类型的 XSD Gen 类

xml - XML模式头和命名空间配置

XSD:元素和属性的区别

java - Spring 安全 AngularJS 禁止 403

java - Jaxb:如何生成 ObjectFactory 类?

java - 响应中没有 namespace 的 SOAP(Spring Boot)

java - jaxb,子元素的单独命名空间