Setting JAXB 2 Context in Jersey 1.17

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 { // Shouldn't get this far if the XML content in the request was invalid System.out.println(dto); } 

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) { // This indicates JAXB that it should continue processing only if the // severity level is less than error. NOTE: validation event constants // go in ascending order in level of severity(ie, 0 WARNING, 1: ERROR, 2 :FATAL_ERROR) return event.getSeverity() < ValidationEvent.ERROR; } } 

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?

+4
source share
1 answer

The Jersey user guide covers this in Using the JAXBContext Custom Chapter . Basically you need to provide a ContextResolver <T> , for example:

 @Provider public class PlanetJAXBContextProvider implements ContextResolver<JAXBContext> { private JAXBContext context = null; public JAXBContext getContext(Class<?> type) { if(type != Planet.class) return null; // we don't support nothing else than Planet if(context == null) { try { context = JAXBContext.newInstance(Planet.class); } catch (JAXBException e) { // log warning/error; null will be returned which indicates that this // provider won't/can't be used. } } return context; } } 

You can see an example of use in the storage-service example project (see JAXBContextResolver ).

Note. Instead of ContextResolver<JAXBContext> you can also specify ContextResolver<Marshaller> and / or ContextResolver<Unmarshaller> .

+3
source

Source: https://habr.com/ru/post/1498893/


All Articles