SPR-7931 - Checking for @XmlRootElement annotation should be made optional in Jaxb2Marshaller

This commit is contained in:
Arjen Poutsma 2011-06-14 09:46:18 +00:00
parent fa0b683161
commit 01d2082090
2 changed files with 36 additions and 9 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -158,6 +158,8 @@ public class Jaxb2Marshaller
private boolean lazyInit = false;
private boolean supportJaxbElementClass = false;
/**
* Set multiple JAXB context paths. The given array of context paths is converted to a
@ -301,6 +303,22 @@ public class Jaxb2Marshaller
this.lazyInit = lazyInit;
}
/**
* Specify whether the {@link #supports(Class)} returns {@code true} for the {@link JAXBElement} class.
* <p>Default is {@code false}, meaning that {@code supports(Class)} always returns {@code false} for
* {@code JAXBElement} classes (though {@link #supports(Type)} can return {@code true}, since it can obtain the
* type parameters of {@code JAXBElement}).
* <p>This property is typically enabled in combination with usage of classes like
* {@link org.springframework.web.servlet.view.xml.MarshallingView MarshallingView}, since the {@code ModelAndView}
* does not offer type parameter information at runtime.
*
* @see #supports(Class)
* @see #supports(Type)
*/
public void setSupportJaxbElementClass(boolean supportJaxbElementClass) {
this.supportJaxbElementClass = supportJaxbElementClass;
}
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;
}
@ -394,6 +412,9 @@ public class Jaxb2Marshaller
public boolean supports(Class<?> clazz) {
if (supportJaxbElementClass && JAXBElement.class.isAssignableFrom(clazz)) {
return true;
}
return supportsInternal(clazz, true);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -27,15 +27,11 @@ import javax.activation.FileDataSource;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.namespace.QName;
import javax.xml.transform.Result;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamResult;
import static org.custommonkey.xmlunit.XMLAssert.assertFalse;
import static org.custommonkey.xmlunit.XMLAssert.*;
import static org.custommonkey.xmlunit.XMLAssert.fail;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
@ -54,6 +50,12 @@ import org.springframework.oxm.mime.MimeContainer;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.ReflectionUtils;
import static org.custommonkey.xmlunit.XMLAssert.assertFalse;
import static org.custommonkey.xmlunit.XMLAssert.*;
import static org.custommonkey.xmlunit.XMLAssert.fail;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertTrue;
public class Jaxb2MarshallerTests extends AbstractMarshallerTests {
private static final String CONTEXT_PATH = "org.springframework.oxm.jaxb.test";
@ -105,7 +107,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests {
}
@Test
public void laxyInit() throws Exception {
public void lazyInit() throws Exception {
marshaller = new Jaxb2Marshaller();
marshaller.setContextPath(CONTEXT_PATH);
marshaller.setLazyInit(true);
@ -152,7 +154,6 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests {
@Test
public void supportsContextPath() throws Exception {
testSupports();
}
@Test
@ -174,6 +175,11 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests {
assertTrue("Jaxb2Marshaller does not support JAXBElement<FlightsType>",
marshaller.supports(method.getGenericReturnType()));
marshaller.setSupportJaxbElementClass(true);
JAXBElement<FlightType> flightTypeJAXBElement = new JAXBElement<FlightType>(new QName("http://springframework.org", "flight"), FlightType.class,
new FlightType());
assertTrue("Jaxb2Marshaller does not support JAXBElement<FlightsType>", marshaller.supports(flightTypeJAXBElement.getClass()));
assertFalse("Jaxb2Marshaller supports class not in context path", marshaller.supports(DummyRootElement.class));
assertFalse("Jaxb2Marshaller supports type not in context path", marshaller.supports((Type)DummyRootElement.class));
method = getClass().getDeclaredMethod("createDummyRootElement");