Reputation: 1105
I have a number of REST resource classes that return a model entity and rely upon JAXRS to convert to XML automatically (without my own custom Provider). I want to be able to access the JAXB marshaller instance used for this so I can configure a ValidationEventHandler to catch exceptions. How do I do this?
Here is my sample entity resource:
@Path("/device")
public class DeviceResource extends CaBridgeServletResourceManager {
/**
* Get the server status.
*/
@GET
@Path("/config")
public DeviceConfigurationResponse getDeviceConfigurationResponse() {
DeviceService service = new DeviceService(getSessionContext());
DeviceConfigurationResponse response = service.createConfigurationResponse(getDeviceCredential());
return response;
}
}
I want to be able to do something like:
Marshaller marshaller = ... get jaxrs default marshaller ...
marshaller.setEventHandler(new MyMarshallerEventHandler());
How do I get the default marshaller used by jaxrs? Or is there a new marshaller instance I can access for each instance of my resource class (above)?
I would rather avoid creating custom Provider classes for every entity class I have.
Upvotes: 1
Views: 273
Reputation: 6497
Define a ContextResolver and it will get used:
@Provider
public class JaxbMarshallerProvider implements ContextResolver<Marshaller> {
@Override
public Marshaller getContext(Class<?> type) {
}
}
And the same thing for the Unmarshaller. We generally instantiate the JAXBContext once and stash it in a static member in the provider class.
Upvotes: 1