Optional @XmlRootElement check in Jaxb2Marshaller

Before this commit, the Jaxb2Marshaller required all supported classes
to be annotated with @XmlRootElement. This commit adds a property
'checkForXmlRootElement' (default is true) which allows for un-annotated
classes.

This is especially useful for JAXB2 implementations that can use
external binding files, such as EclipseLink MOXy.

Issue: SPR-9757
This commit is contained in:
Arjen Poutsma 2012-09-06 13:12:03 +02:00
parent 6517517ca9
commit bd018fc9d7
1 changed files with 19 additions and 2 deletions

View File

@ -168,6 +168,8 @@ public class Jaxb2Marshaller
private boolean supportJaxbElementClass = false;
private boolean checkForXmlRootElement = true;
private LSResourceResolver schemaResourceResolver;
@ -358,6 +360,21 @@ public class Jaxb2Marshaller
this.supportJaxbElementClass = supportJaxbElementClass;
}
/**
* Specify whether the {@link #supports(Class)} should check for
* {@link XmlRootElement @XmlRootElement} annotations.
* <p>Default is {@code true}, meaning that {@code supports(Class)} will check for
* this annotation. However, some JAXB implementations (i.e. EclipseLink MOXy) allow
* for defining the bindings in an external definition file, thus keeping the classes
* annotations free. Setting this property to {@code false} supports these
* JAXB implementations.
* @see #supports(Class)
* @see #supports(Type)
*/
public void setCheckForXmlRootElement(boolean checkForXmlRootElement) {
this.checkForXmlRootElement = checkForXmlRootElement;
}
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;
}
@ -492,7 +509,7 @@ public class Jaxb2Marshaller
if (this.supportJaxbElementClass && JAXBElement.class.isAssignableFrom(clazz)) {
return true;
}
return supportsInternal(clazz, true);
return supportsInternal(clazz, this.checkForXmlRootElement);
}
public boolean supports(Type genericType) {
@ -521,7 +538,7 @@ public class Jaxb2Marshaller
}
else if (genericType instanceof Class) {
Class<?> clazz = (Class<?>) genericType;
return supportsInternal(clazz, true);
return supportsInternal(clazz, this.checkForXmlRootElement);
}
return false;
}