Java Reference
In-Depth Information
Validating Your Payload Against a Schema on the
Client
Problem
You want to consume a web service whose schema defines constraints (such as min and max
length of strings, or pattern restrictions using regular expressions), and you want your gener-
ated Java objects on the client to conform to that schema; however, when you use wsimport ,
the generated objects have “forgotten” the constraints. You want to do this in a portable way,
without using vendor extensions.
Solution
Populate the Java objects in your client application as usual, and then create a JAXB Mar-
shaller and set the schema reference on it. Then, when it's time to invoke the service, pass
a populated Java object representing the generated parameter type to a Dispatch that uses the
marshaler to populate the payload. JAXB will do the validation for you.
Discussion
The basic problem that this recipe addresses is this: you want to invoke a service whose
schema defines constraints that your payloads must conform to, but those constraints are no
longer present in Java objects that have been generated by JAXB from that schema. When it's
time to invoke your service, there's nothing to ensure that the data populating your object ac-
tually matches the schema-defined constraints. You don't want to duplicate those constraints
elsewhere, but you do want to make sure that your service is passed only valid objects that ac-
tually conform to the schema. So, you need a way to validate those objects. While you could
hope that the service does validation (and it should), you don't want to incur unnecessary net-
work overhead if someone just fat-fingered a phone number.
NOTE
This recipe shows you how to do this by hand if you are not using the latest version of JAX-
WS. If you are, however, it's easier to do it by using the
com.sun.xml.ws.developer.SchemaValidation feature, specifying a handler for it, and
passing that to your proxy. This is specific to Metro 1.1, however, and is not portable.
Search WWH ::




Custom Search