5
votes

I am implementing java-first web services using JAX-WS and trying to use the @SchemaValidation to validate required elements that are in my webMethod parameters. here is how I declare my required attribute;

@XmlElement(required=true) maps my wsdl schema perfectly(minOccur is not 0 anymore) but the schemaValidation in not validating the passed null elements.

@XmlElement(required=true, nillable=false)
public String getClubName() {
    return clubName;
}

Any client that uses my API can pass null elements. I don't have a way to prevent this using JAXB.

On the other hand bean validation is working as follows. However not for String fields

@Column(name = "FOUNDATION_YEAR", nullable = false) 
private Integer foundationYear;   //schemaValidation validates

@Column(name = "CLUB_NAME", length=100, nullable = false)
private String clubName;    //schemaValidation does not validate

How can I use schemaValidation to validate string values? One way is to put restrictions but you can not do that when using java-first web services

<xsd:simpleType name="NotEmptyString">
  <xsd:restriction base="xsd:string">
    <xsd:minLength  value="1"/>
  </xsd:restriction>
</xsd:simpleType>

related posts;

Validation for generated JAXB Classes (JSR 303 / Spring)

How to enable schema validation so that JAXB rejects empty element?

1

1 Answers

0
votes

Apparently you can do it using JAXB like said here. Or maybe use the Schema Validation Framework as described here. But, IMHO, both the approaches add too much clutter to the code, so I'd go for Guava's Preconditions, which gives you a fast and clean way of doing this. Too bad there's no automatic support for this =/.