diff --git a/org.springframework.oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java b/org.springframework.oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java index a98c2a6acdd..6720d512da6 100644 --- a/org.springframework.oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java +++ b/org.springframework.oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java @@ -177,7 +177,7 @@ public class Jaxb2Marshaller * Set multiple JAXB context paths. The given array of context paths is converted to a * colon-delimited string, as supported by JAXB. */ - public void setContextPaths(String[] contextPaths) { + public void setContextPaths(String... contextPaths) { Assert.notEmpty(contextPaths, "'contextPaths' must not be empty"); this.contextPath = StringUtils.arrayToDelimitedString(contextPaths, ":"); } @@ -193,7 +193,8 @@ public class Jaxb2Marshaller * Set the list of Java classes to be recognized by a newly created JAXBContext. * Setting this property or {@link #setContextPath "contextPath"} is required. */ - public void setClassesToBeBound(Class[] classesToBeBound) { + public void setClassesToBeBound(Class... classesToBeBound) { + Assert.notEmpty(classesToBeBound, "'classesToBeBound' must not be empty"); this.classesToBeBound = classesToBeBound; } diff --git a/org.springframework.oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb2MarshallerTests.java b/org.springframework.oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb2MarshallerTests.java index 0e9771dbe96..96094c8a480 100644 --- a/org.springframework.oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb2MarshallerTests.java +++ b/org.springframework.oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb2MarshallerTests.java @@ -142,7 +142,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests { @Test(expected = XmlMappingException.class) public void marshalInvalidClass() throws Exception { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); - marshaller.setClassesToBeBound(new Class[]{FlightType.class}); + marshaller.setClassesToBeBound(FlightType.class); marshaller.afterPropertiesSet(); Result result = new StreamResult(new StringWriter()); Flights flights = new Flights(); @@ -158,7 +158,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests { @Test public void supportsClassesToBeBound() throws Exception { marshaller = new Jaxb2Marshaller(); - marshaller.setClassesToBeBound(new Class[]{Flights.class, FlightType.class}); + marshaller.setClassesToBeBound(Flights.class, FlightType.class); marshaller.afterPropertiesSet(); testSupports(); } @@ -239,7 +239,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests { @Test public void supportsXmlRootElement() throws Exception { marshaller = new Jaxb2Marshaller(); - marshaller.setClassesToBeBound(new Class[]{DummyRootElement.class, DummyType.class}); + marshaller.setClassesToBeBound(DummyRootElement.class, DummyType.class); marshaller.afterPropertiesSet(); assertTrue("Jaxb2Marshaller does not support XmlRootElement class", marshaller.supports(DummyRootElement.class)); assertTrue("Jaxb2Marshaller does not support XmlRootElement generic type", marshaller.supports((Type)DummyRootElement.class)); @@ -252,7 +252,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests { @Test public void marshalAttachments() throws Exception { marshaller = new Jaxb2Marshaller(); - marshaller.setClassesToBeBound(new Class[]{BinaryObject.class}); + marshaller.setClassesToBeBound(BinaryObject.class); marshaller.setMtomEnabled(true); marshaller.afterPropertiesSet(); MimeContainer mimeContainer = createMock(MimeContainer.class); diff --git a/org.springframework.oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb2UnmarshallerTests.java b/org.springframework.oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb2UnmarshallerTests.java index 95482cbe68e..8b4ce76a802 100644 --- a/org.springframework.oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb2UnmarshallerTests.java +++ b/org.springframework.oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb2UnmarshallerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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. @@ -57,7 +57,7 @@ public class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests { @Test public void marshalAttachments() throws Exception { unmarshaller = new Jaxb2Marshaller(); - unmarshaller.setClassesToBeBound(new Class[]{BinaryObject.class}); + unmarshaller.setClassesToBeBound(BinaryObject.class); unmarshaller.setMtomEnabled(true); unmarshaller.afterPropertiesSet(); MimeContainer mimeContainer = createMock(MimeContainer.class); diff --git a/org.springframework.web/src/main/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverter.java b/org.springframework.web/src/main/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverter.java index 97770b6e68e..a5709921670 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverter.java +++ b/org.springframework.web/src/main/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverter.java @@ -20,6 +20,7 @@ import java.io.IOException; import javax.xml.transform.Result; import javax.xml.transform.Source; +import org.springframework.beans.TypeMismatchException; import org.springframework.http.HttpHeaders; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotWritableException; @@ -110,7 +111,11 @@ public class MarshallingHttpMessageConverter extends AbstractXmlHttpMessageConve protected Object readFromSource(Class clazz, HttpHeaders headers, Source source) throws IOException { Assert.notNull(this.unmarshaller, "Property 'unmarshaller' is required"); try { - return this.unmarshaller.unmarshal(source); + Object result = this.unmarshaller.unmarshal(source); + if (!clazz.isInstance(result)) { + throw new TypeMismatchException(result, clazz); + } + return result; } catch (UnmarshallingFailureException ex) { throw new HttpMessageNotReadableException("Could not read [" + clazz + "]", ex);