Skip to content

Commit bd018fc

Browse files
committed
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
1 parent 6517517 commit bd018fc

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ public class Jaxb2Marshaller
168168

169169
private boolean supportJaxbElementClass = false;
170170

171+
private boolean checkForXmlRootElement = true;
172+
171173
private LSResourceResolver schemaResourceResolver;
172174

173175

@@ -358,6 +360,21 @@ public void setSupportJaxbElementClass(boolean supportJaxbElementClass) {
358360
this.supportJaxbElementClass = supportJaxbElementClass;
359361
}
360362

363+
/**
364+
* Specify whether the {@link #supports(Class)} should check for
365+
* {@link XmlRootElement @XmlRootElement} annotations.
366+
* <p>Default is {@code true}, meaning that {@code supports(Class)} will check for
367+
* this annotation. However, some JAXB implementations (i.e. EclipseLink MOXy) allow
368+
* for defining the bindings in an external definition file, thus keeping the classes
369+
* annotations free. Setting this property to {@code false} supports these
370+
* JAXB implementations.
371+
* @see #supports(Class)
372+
* @see #supports(Type)
373+
*/
374+
public void setCheckForXmlRootElement(boolean checkForXmlRootElement) {
375+
this.checkForXmlRootElement = checkForXmlRootElement;
376+
}
377+
361378
public void setBeanClassLoader(ClassLoader classLoader) {
362379
this.beanClassLoader = classLoader;
363380
}
@@ -492,7 +509,7 @@ public boolean supports(Class<?> clazz) {
492509
if (this.supportJaxbElementClass && JAXBElement.class.isAssignableFrom(clazz)) {
493510
return true;
494511
}
495-
return supportsInternal(clazz, true);
512+
return supportsInternal(clazz, this.checkForXmlRootElement);
496513
}
497514

498515
public boolean supports(Type genericType) {
@@ -521,7 +538,7 @@ else if (JdkVersion.getMajorJavaVersion() <= JdkVersion.JAVA_16 &&
521538
}
522539
else if (genericType instanceof Class) {
523540
Class<?> clazz = (Class<?>) genericType;
524-
return supportsInternal(clazz, true);
541+
return supportsInternal(clazz, this.checkForXmlRootElement);
525542
}
526543
return false;
527544
}

0 commit comments

Comments
 (0)