Reputation: 673
I'm going to create ID
field in my Java code using UUID
. I need to create xml from my Book
class and validate it based on below XSD
.
My XSD
looks like this
<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>
My Java class looks like this
@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;
}
How should I implement my GUID
class to return a UUID.randomUUID()
or any other way to pass the XSD
?
Upvotes: 0
Views: 785
Reputation: 4448
Take a look at this example, basically you have to annotate the method which return the xml value with @XmlValue
public class GUID {
private final UUID uuid;
public GUID() {
this.uuid = UUID.randomUUID();
}
@XmlValue
public String getValue(){
return uuid.toString();
}
}
Upvotes: 1