JAXB @XmlElement with string length limit (generated XSD simple type)

4.5k Views Asked by At

JAXB @XmlElement with string length limit (generated XSD simple type)

I use Java to generate a SOAP webservice. Therefore, this is a code first approach

In Java I have a String field, on which I like to have a length limit of 50 characters.

@XmlRootElement
public class ContactTO {

  private String comment; 

  @XmlElement
  public void setComment(String comment) {
    this.comment = comment;
  }
}

public class ContactWebService implements ContactServiceIf {

  @Override
  public ContactTO createContact(@WebParam(name = "newContact") ContactTO newContact)
  ...

http://localhost:12080/services/contactService?wsdl

Is showing:

<xs:complexType name="contactTO">
 <xs:sequence>
  <xs:element minOccurs="0" name="comment" type="xs:string" /> 

How can I limit the comment element to a length of 50?

I'd like to have something like this:

  <xs:simpleType name="LimitedString">
    <xs:restriction base="xs:string">
      <xs:maxLength value="50" />
    </xs:restriction>
  </xs:simpleType>

Or something like this:

<xs:simpleType name="fourCharAlphaString">
  <xs:restriction base="xs:string">
    <xs:pattern value="[a-zA-Z]{4}"/>
  </xs:restriction>
</xs:simpleType>  

But I can't manually edit the inline (generated) xsd in the wsdl.

Thank you very much.

(note: XSD: how to restrict number of characters in string type attribute?)

(note: http://www.w3schools.com/schema/schema_facets.asp)

Thanks, Reto

0

There are 0 best solutions below