I use Jersey 1.17 on the server side to handle REST and JAXB 2 requests to decouple the contents of the XML request.
Context
This is the jersey method that I use. The MyDTO class uses the @XmlRootElement annotation (otherwise I will need to define a parameter of type JAXBElement).
@Path("/myService") @POST @Consumes(MediaType.APPLICATION_XML) public void myService(MyDTO dto) throws Exception {
Demand
By default, the Sun / Oracle JAXB implementation does not throw an exception when the XML content contains errors. For example, providing a string value, such as ABC, for the Integer attribute simply leaves the value as null instead of throwing an exception.
In JAXB 2, a ValidationEvenHandler can be defined. Using the following handler handler, it makes the XML marking different from the exception the way I need it.
public class UnmarshallerValidationEventHandler implements ValidationEventHandler { @Override public boolean handleEvent(ValidationEvent event) {
Question
How can I get Jersey to use a specific instance of JAXBContext to use unmarshaller with my custom validation event handler?
Alternatively, given that my application only uses JAXB in Jersey methods, defining a specific JAXBContext globally for a JVM instance would be a good option. How can I do that?
source share