On Aug 8, 2009, at 1:49 PM, MS wrote:
> Hi,
>
> I am posting an XML-resource to a Jersey resource class that uses
> JAXB to unmarshal XML to a Java object.
>
> The method from the resource class looks like this:
>
> @POST
> @Consumes("application/xml")
> public Response doSomething( MyJaxbClass myJaxbClass ){
> ...
> }
>
> and the class with JAXB annotations like this:
> @XmlType(propOrder = {"id", "name"})
> @XmlRootElement
> public class DocumentServiceProvider{
> ...
> }
>
> The unmarshalling works correctly, but it does not care about the
> order of the XML elements. Has anyone an idea what I am doing wrong?
>
JAXB does not, by default, validate the XML.
IIRC the internal model that JAXB uses for parsing is not as strict as
the XSD produced from any JAXB beans. So child elements are treated as
a "bag" without a specific order (although i am not sure how it
handles sequences in this respect). Also JAXB will ignore any elements
it does not know about. IMHO this is an advantage because it means
JAXB can cope with differences (there is no one Schema, and clients/
services can often diverge).
To configure validation you will need to use a ContextResolver [1]
using either JAXBContext or a Unmarshaller, that returns an
Unmarshaller for the types you want to validate with validation enabled.
See the following to enable JAXB validation:
https://jaxb.dev.java.net/tutorial/section_3_3-Validation.html
Then you can create a provider class as follows:
@Provider
public class MyResolver implements ContextResolver<Unmarshaller> {
// Remeber JAXBContext instances are expensive to create, so
create once and reuse
public Unmarshaller getContext(java.lang.Class<?> type) {
if (/ type is something that i support and should be validated
for unmarshalling/) {
Unmarshaller u = ... // create unmarshaller with validation
enabled
return u;
} else {
return null;
}
}
}
Paul.
[1]
https://jsr311.dev.java.net/nonav/javadoc/javax/ws/rs/ext/ContextResolver.html
> Thanks!
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>