如何使用 JAXB 在我的 Java 代码中创建 UUID 以传递 XSD 模式限制
How to create UUID in my Java code using JAXB to pass XSD pattern restriction
我将使用 UUID
在我的 Java 代码中创建 ID
字段。我需要从我的 Book
class 创建 xml 并根据下面的 XSD
验证它。
我的XSD
看起来像这样
<xsd:complexType name="Book" >
<xsd:sequence>
<xsd:element name="Publisher" type="ns:PublisherType"/>
<xsd:element name="MessageId" type="ns:GUID"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Title" type="xsd:string"/>
<xsd:any processContents="lax" minOccurs="0" maxOccurs="unbounded"
namespace="##other"/>
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="GUID">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}"/>
</xsd:restriction>
</xsd:simpleType>
我的Javaclass看起来像这样
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
@XmlType(name="Book", propOrder = {"publisher", "messageId", "author", "title"
})
@Getter
@Setter
public class Book {
private Publisher publisher;
private GUID messageId;
private String author;
private String title;
}
我应该如何实现我的 GUID
class 到 return 一个 UUID.randomUUID()
或者任何其他方式来传递 XSD
?
看看this example,基本上你必须用@XmlValue
注释return xml值的方法
public class GUID {
private final UUID uuid;
public GUID() {
this.uuid = UUID.randomUUID();
}
@XmlValue
public String getValue(){
return uuid.toString();
}
}
我将使用 UUID
在我的 Java 代码中创建 ID
字段。我需要从我的 Book
class 创建 xml 并根据下面的 XSD
验证它。
我的XSD
看起来像这样
<xsd:complexType name="Book" >
<xsd:sequence>
<xsd:element name="Publisher" type="ns:PublisherType"/>
<xsd:element name="MessageId" type="ns:GUID"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Title" type="xsd:string"/>
<xsd:any processContents="lax" minOccurs="0" maxOccurs="unbounded"
namespace="##other"/>
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="GUID">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}"/>
</xsd:restriction>
</xsd:simpleType>
我的Javaclass看起来像这样
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
@XmlType(name="Book", propOrder = {"publisher", "messageId", "author", "title"
})
@Getter
@Setter
public class Book {
private Publisher publisher;
private GUID messageId;
private String author;
private String title;
}
我应该如何实现我的 GUID
class 到 return 一个 UUID.randomUUID()
或者任何其他方式来传递 XSD
?
看看this example,基本上你必须用@XmlValue
public class GUID {
private final UUID uuid;
public GUID() {
this.uuid = UUID.randomUUID();
}
@XmlValue
public String getValue(){
return uuid.toString();
}
}