From bd018fc9d7f872d1dde46ee528cc2742216ae428 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Thu, 6 Sep 2012 13:12:03 +0200 Subject: [PATCH] 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 --- .../oxm/jaxb/Jaxb2Marshaller.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java b/spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java index dbfb260db2a..62794636d6e 100644 --- a/spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java +++ b/spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java @@ -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. + *

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; }