Trying to seek design suggestion on a schema based request object for a CXF endpoint. I've created a CXF SOAP endpoint which takes the following object as an input. The request object will have required fields, non-mandatory fields as well as user-defined fields.Required fields are enforced through the schema. The tag object is defined as a placeholder for user defined properties. The eventual goal of the endpoint has transformed the values into Tag object and send them for processing.
public class TestServiceRequest{
@XmlElement(required = true)
protected String url;
@XmlElement(required = true)
protected String title;
protected String source;
protected List tags;
public void setUrl(String value) {
this.url = value;
}
public void setTitle(String title) {
this.title = title;
}
public void setSource(String value) {
this.text = value;
}
public List getTags() {
if (tags == null) {
tags = new ArrayList();
}
return this.tags;
}
Tag object :
<pre><code>
public class Tag{
public String name;
public String value;
public String weight;
}
</code></pre>
Schema snippet:
<pre><code>
<xsd:complexType name="tag">
<xsd:sequence>
<xsd:element name="name" type="xsd:string" minOccurs="1" maxOccurs="1" />
<xsd:element name="value" type="xsd:string" minOccurs="1" maxOccurs="1" />
<xsd:element name="weight" type="xsd:string" minOccurs="0" maxOccurs="1" />
<xsd:element name="attribute" type="tns:attribute" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="testServiceRequest">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="br:BaseRequest">
<xsd:sequence>
<xsd:element name="url" type="xsd:string" minOccurs="1" maxOccurs="1"/>
<xsd:element name="title" type="xsd:string" minOccurs="1" maxOccurs="1"/>
<xsd:element name="text" type="xsd:string" minOccurs="1" maxOccurs="1"/>
<xsd:element name="source" type="xsd:string" minOccurs="1" maxOccurs="1"/>
<xsd:element name="tags" type="tns:tag" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</code></pre>
As you can see, Tag object has an additional field called weight, which is the crux of the issue. Weight is an optional field for each property defined as required (url, title) and optional fields (source) as well. The weights are static value defined for these properties.
The reason why I inclined towards this model is first, property field names (URL, title, etc.) conform to a standard and cannot deviate. Having defined them in a schema and generating the request based on it, allows me to impose that. The end-user is free from using the exact field name and just use the setter instead, thus reducing the scope of error. Moreover, schema validation can be integrated with CXF endpoint easily, hence maintenance is easy. Any change is restricted to the schema.
An issue with this model is the service endpoint explicitly needs to add the weight for the fields while converting them to Tag objects. If the client ever decides to send their own weight values specific to properties, then this model will break.
The other option is to use a simplified request object instead of a schema generated one.
<pre><code>
public class Tag{
public String name;
public String value;
public String weight;
}
</code></pre>
The pros are that this model is simple and flexible and allows user to define weights and other metadata if required.
An issue with this, I need an explicit validation code to make sure the required fields are provided and the field names are correct. That means, every time a new field is added, I need to touch the code to add it as part of the validation.
I'm in a dilemma to decide which is the best possible way to design this. Maybe, there's a better way to do this instead.