O'Reilly logo

Java SOA Cookbook by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 generated 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 Marshaller 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 actually 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 actually conform to the schema. So, you need a way to validate those objects. While you could hope that the service does validation ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required