I am using Jersey to create a restful web-service marshals XML.
How would I set the xsi:schemaLocation?
This answer show how to set the Marshaller.JAXB_SCHEMA_LOCATION directly on the Marshaller.
The trouble I am having is that Jersey is marshaling the Java objects into XML. How do I tell Jersey what the schema location is?
You could create a
MessageBodyWriter
for this use case. Through theContextResolver
mechanism you can get theJAXBContext
associated with your domain model. Then you can get aMarshaller
from theJAXBContext
and set theJAXB_SCHEMA_LOCATION
on it and do the marshal.UPDATE
You still implement your resource the same way. The
MessageBodyWriter
mechanism is just a way to override how the writing to XML will be done. The@Provider
annotation is a signal to the JAX-RS application to have this class automatically registered.You could implement it as
MessageBodyWriter<Foo>
if you only want it applied to theFoo
class. If you want it to apply to more than justFoo
you can implement to theisWriteable
method to return true for the appropriate classes.