提交 723f94fd 编写于 作者: A Arjen Poutsma

SPR-7263 - TypeMismatchException instead of IllegalArgumentException: argument...

SPR-7263 - TypeMismatchException instead of IllegalArgumentException: argument type mismatch for wrong RequestBody
上级 2a140add
...@@ -177,7 +177,7 @@ public class Jaxb2Marshaller ...@@ -177,7 +177,7 @@ public class Jaxb2Marshaller
* Set multiple JAXB context paths. The given array of context paths is converted to a * Set multiple JAXB context paths. The given array of context paths is converted to a
* colon-delimited string, as supported by JAXB. * 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"); Assert.notEmpty(contextPaths, "'contextPaths' must not be empty");
this.contextPath = StringUtils.arrayToDelimitedString(contextPaths, ":"); this.contextPath = StringUtils.arrayToDelimitedString(contextPaths, ":");
} }
...@@ -193,7 +193,8 @@ public class Jaxb2Marshaller ...@@ -193,7 +193,8 @@ public class Jaxb2Marshaller
* Set the list of Java classes to be recognized by a newly created JAXBContext. * Set the list of Java classes to be recognized by a newly created JAXBContext.
* Setting this property or {@link #setContextPath "contextPath"} is required. * 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; this.classesToBeBound = classesToBeBound;
} }
......
...@@ -142,7 +142,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests { ...@@ -142,7 +142,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests {
@Test(expected = XmlMappingException.class) @Test(expected = XmlMappingException.class)
public void marshalInvalidClass() throws Exception { public void marshalInvalidClass() throws Exception {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(new Class[]{FlightType.class}); marshaller.setClassesToBeBound(FlightType.class);
marshaller.afterPropertiesSet(); marshaller.afterPropertiesSet();
Result result = new StreamResult(new StringWriter()); Result result = new StreamResult(new StringWriter());
Flights flights = new Flights(); Flights flights = new Flights();
...@@ -158,7 +158,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests { ...@@ -158,7 +158,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests {
@Test @Test
public void supportsClassesToBeBound() throws Exception { public void supportsClassesToBeBound() throws Exception {
marshaller = new Jaxb2Marshaller(); marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(new Class[]{Flights.class, FlightType.class}); marshaller.setClassesToBeBound(Flights.class, FlightType.class);
marshaller.afterPropertiesSet(); marshaller.afterPropertiesSet();
testSupports(); testSupports();
} }
...@@ -239,7 +239,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests { ...@@ -239,7 +239,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests {
@Test @Test
public void supportsXmlRootElement() throws Exception { public void supportsXmlRootElement() throws Exception {
marshaller = new Jaxb2Marshaller(); marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(new Class[]{DummyRootElement.class, DummyType.class}); marshaller.setClassesToBeBound(DummyRootElement.class, DummyType.class);
marshaller.afterPropertiesSet(); marshaller.afterPropertiesSet();
assertTrue("Jaxb2Marshaller does not support XmlRootElement class", marshaller.supports(DummyRootElement.class)); assertTrue("Jaxb2Marshaller does not support XmlRootElement class", marshaller.supports(DummyRootElement.class));
assertTrue("Jaxb2Marshaller does not support XmlRootElement generic type", marshaller.supports((Type)DummyRootElement.class)); assertTrue("Jaxb2Marshaller does not support XmlRootElement generic type", marshaller.supports((Type)DummyRootElement.class));
...@@ -252,7 +252,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests { ...@@ -252,7 +252,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests {
@Test @Test
public void marshalAttachments() throws Exception { public void marshalAttachments() throws Exception {
marshaller = new Jaxb2Marshaller(); marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(new Class[]{BinaryObject.class}); marshaller.setClassesToBeBound(BinaryObject.class);
marshaller.setMtomEnabled(true); marshaller.setMtomEnabled(true);
marshaller.afterPropertiesSet(); marshaller.afterPropertiesSet();
MimeContainer mimeContainer = createMock(MimeContainer.class); MimeContainer mimeContainer = createMock(MimeContainer.class);
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -57,7 +57,7 @@ public class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests { ...@@ -57,7 +57,7 @@ public class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests {
@Test @Test
public void marshalAttachments() throws Exception { public void marshalAttachments() throws Exception {
unmarshaller = new Jaxb2Marshaller(); unmarshaller = new Jaxb2Marshaller();
unmarshaller.setClassesToBeBound(new Class[]{BinaryObject.class}); unmarshaller.setClassesToBeBound(BinaryObject.class);
unmarshaller.setMtomEnabled(true); unmarshaller.setMtomEnabled(true);
unmarshaller.afterPropertiesSet(); unmarshaller.afterPropertiesSet();
MimeContainer mimeContainer = createMock(MimeContainer.class); MimeContainer mimeContainer = createMock(MimeContainer.class);
......
...@@ -20,6 +20,7 @@ import java.io.IOException; ...@@ -20,6 +20,7 @@ import java.io.IOException;
import javax.xml.transform.Result; import javax.xml.transform.Result;
import javax.xml.transform.Source; import javax.xml.transform.Source;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException; import org.springframework.http.converter.HttpMessageNotWritableException;
...@@ -110,7 +111,11 @@ public class MarshallingHttpMessageConverter extends AbstractXmlHttpMessageConve ...@@ -110,7 +111,11 @@ public class MarshallingHttpMessageConverter extends AbstractXmlHttpMessageConve
protected Object readFromSource(Class<?> clazz, HttpHeaders headers, Source source) throws IOException { protected Object readFromSource(Class<?> clazz, HttpHeaders headers, Source source) throws IOException {
Assert.notNull(this.unmarshaller, "Property 'unmarshaller' is required"); Assert.notNull(this.unmarshaller, "Property 'unmarshaller' is required");
try { 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) { catch (UnmarshallingFailureException ex) {
throw new HttpMessageNotReadableException("Could not read [" + clazz + "]", ex); throw new HttpMessageNotReadableException("Could not read [" + clazz + "]", ex);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册