SPR-7263 - TypeMismatchException instead of IllegalArgumentException: argument type mismatch for wrong RequestBody

This commit is contained in:
Arjen Poutsma 2010-06-09 10:35:41 +00:00
parent 2a140addfd
commit 723f94fd0e
4 changed files with 15 additions and 9 deletions

View File

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

View File

@ -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);

View File

@ -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);

View File

@ -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);