Skip to content

Commit 723f94f

Browse files
committed
SPR-7263 - TypeMismatchException instead of IllegalArgumentException: argument type mismatch for wrong RequestBody
1 parent 2a140ad commit 723f94f

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public void setContextPath(String contextPath) {
177177
* Set multiple JAXB context paths. The given array of context paths is converted to a
178178
* colon-delimited string, as supported by JAXB.
179179
*/
180-
public void setContextPaths(String[] contextPaths) {
180+
public void setContextPaths(String... contextPaths) {
181181
Assert.notEmpty(contextPaths, "'contextPaths' must not be empty");
182182
this.contextPath = StringUtils.arrayToDelimitedString(contextPaths, ":");
183183
}
@@ -193,7 +193,8 @@ public Class<?>[] getClassesToBeBound() {
193193
* Set the list of Java classes to be recognized by a newly created JAXBContext.
194194
* Setting this property or {@link #setContextPath "contextPath"} is required.
195195
*/
196-
public void setClassesToBeBound(Class<?>[] classesToBeBound) {
196+
public void setClassesToBeBound(Class<?>... classesToBeBound) {
197+
Assert.notEmpty(classesToBeBound, "'classesToBeBound' must not be empty");
197198
this.classesToBeBound = classesToBeBound;
198199
}
199200

org.springframework.oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb2MarshallerTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public void testInvalidContextPath() throws Exception {
142142
@Test(expected = XmlMappingException.class)
143143
public void marshalInvalidClass() throws Exception {
144144
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
145-
marshaller.setClassesToBeBound(new Class[]{FlightType.class});
145+
marshaller.setClassesToBeBound(FlightType.class);
146146
marshaller.afterPropertiesSet();
147147
Result result = new StreamResult(new StringWriter());
148148
Flights flights = new Flights();
@@ -158,7 +158,7 @@ public void supportsContextPath() throws Exception {
158158
@Test
159159
public void supportsClassesToBeBound() throws Exception {
160160
marshaller = new Jaxb2Marshaller();
161-
marshaller.setClassesToBeBound(new Class[]{Flights.class, FlightType.class});
161+
marshaller.setClassesToBeBound(Flights.class, FlightType.class);
162162
marshaller.afterPropertiesSet();
163163
testSupports();
164164
}
@@ -239,7 +239,7 @@ public boolean matches(Method method) {
239239
@Test
240240
public void supportsXmlRootElement() throws Exception {
241241
marshaller = new Jaxb2Marshaller();
242-
marshaller.setClassesToBeBound(new Class[]{DummyRootElement.class, DummyType.class});
242+
marshaller.setClassesToBeBound(DummyRootElement.class, DummyType.class);
243243
marshaller.afterPropertiesSet();
244244
assertTrue("Jaxb2Marshaller does not support XmlRootElement class", marshaller.supports(DummyRootElement.class));
245245
assertTrue("Jaxb2Marshaller does not support XmlRootElement generic type", marshaller.supports((Type)DummyRootElement.class));
@@ -252,7 +252,7 @@ public void supportsXmlRootElement() throws Exception {
252252
@Test
253253
public void marshalAttachments() throws Exception {
254254
marshaller = new Jaxb2Marshaller();
255-
marshaller.setClassesToBeBound(new Class[]{BinaryObject.class});
255+
marshaller.setClassesToBeBound(BinaryObject.class);
256256
marshaller.setMtomEnabled(true);
257257
marshaller.afterPropertiesSet();
258258
MimeContainer mimeContainer = createMock(MimeContainer.class);

org.springframework.oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb2UnmarshallerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2010 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,7 +57,7 @@ public Unmarshaller createUnmarshaller() throws Exception {
5757
@Test
5858
public void marshalAttachments() throws Exception {
5959
unmarshaller = new Jaxb2Marshaller();
60-
unmarshaller.setClassesToBeBound(new Class[]{BinaryObject.class});
60+
unmarshaller.setClassesToBeBound(BinaryObject.class);
6161
unmarshaller.setMtomEnabled(true);
6262
unmarshaller.afterPropertiesSet();
6363
MimeContainer mimeContainer = createMock(MimeContainer.class);

org.springframework.web/src/main/java/org/springframework/http/converter/xml/MarshallingHttpMessageConverter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import javax.xml.transform.Result;
2121
import javax.xml.transform.Source;
2222

23+
import org.springframework.beans.TypeMismatchException;
2324
import org.springframework.http.HttpHeaders;
2425
import org.springframework.http.converter.HttpMessageNotReadableException;
2526
import org.springframework.http.converter.HttpMessageNotWritableException;
@@ -110,7 +111,11 @@ public boolean supports(Class<?> clazz) {
110111
protected Object readFromSource(Class<?> clazz, HttpHeaders headers, Source source) throws IOException {
111112
Assert.notNull(this.unmarshaller, "Property 'unmarshaller' is required");
112113
try {
113-
return this.unmarshaller.unmarshal(source);
114+
Object result = this.unmarshaller.unmarshal(source);
115+
if (!clazz.isInstance(result)) {
116+
throw new TypeMismatchException(result, clazz);
117+
}
118+
return result;
114119
}
115120
catch (UnmarshallingFailureException ex) {
116121
throw new HttpMessageNotReadableException("Could not read [" + clazz + "]", ex);

0 commit comments

Comments
 (0)