Skip to content

Commit 65885d1

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

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationControllerTests.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import javax.servlet.http.HttpSession;
4949
import javax.validation.Valid;
5050
import javax.validation.constraints.NotNull;
51+
import javax.xml.bind.annotation.XmlRootElement;
5152

5253
import static org.junit.Assert.*;
5354
import org.junit.Test;
@@ -60,6 +61,7 @@
6061
import org.springframework.beans.GenericBean;
6162
import org.springframework.beans.ITestBean;
6263
import org.springframework.beans.TestBean;
64+
import org.springframework.beans.factory.BeanCreationException;
6365
import org.springframework.beans.factory.annotation.Autowired;
6466
import org.springframework.beans.factory.annotation.Value;
6567
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
@@ -81,10 +83,12 @@
8183
import org.springframework.http.converter.HttpMessageNotReadableException;
8284
import org.springframework.http.converter.HttpMessageNotWritableException;
8385
import org.springframework.http.converter.StringHttpMessageConverter;
86+
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
8487
import org.springframework.mock.web.MockHttpServletRequest;
8588
import org.springframework.mock.web.MockHttpServletResponse;
8689
import org.springframework.mock.web.MockServletConfig;
8790
import org.springframework.mock.web.MockServletContext;
91+
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
8892
import org.springframework.stereotype.Controller;
8993
import org.springframework.ui.ExtendedModelMap;
9094
import org.springframework.ui.Model;
@@ -1256,6 +1260,44 @@ public void responseBodyVoid() throws ServletException, IOException {
12561260
assertEquals(200, response.getStatus());
12571261
}
12581262

1263+
@Test
1264+
public void responseBodyArgMismatch() throws ServletException, IOException {
1265+
@SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() {
1266+
@Override
1267+
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
1268+
GenericWebApplicationContext wac = new GenericWebApplicationContext();
1269+
wac.registerBeanDefinition("controller", new RootBeanDefinition(RequestBodyArgMismatchController.class));
1270+
1271+
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
1272+
marshaller.setClassesToBeBound(A.class, B.class);
1273+
try {
1274+
marshaller.afterPropertiesSet();
1275+
}
1276+
catch (Exception ex) {
1277+
throw new BeanCreationException(ex.getMessage(), ex);
1278+
}
1279+
1280+
MarshallingHttpMessageConverter messageConverter = new MarshallingHttpMessageConverter(marshaller);
1281+
1282+
RootBeanDefinition adapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class);
1283+
adapterDef.getPropertyValues().add("messageConverters", messageConverter);
1284+
wac.registerBeanDefinition("handlerAdapter", adapterDef);
1285+
wac.refresh();
1286+
return wac;
1287+
}
1288+
};
1289+
servlet.init(new MockServletConfig());
1290+
1291+
1292+
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
1293+
String requestBody = "<b/>";
1294+
request.setContent(requestBody.getBytes("UTF-8"));
1295+
request.addHeader("Content-Type", "application/xml; charset=utf-8");
1296+
MockHttpServletResponse response = new MockHttpServletResponse();
1297+
servlet.service(request, response);
1298+
assertEquals(400, response.getStatus());
1299+
}
1300+
12591301

12601302
@Test
12611303
public void contentTypeHeaders() throws ServletException, IOException {
@@ -2299,6 +2341,25 @@ public void handle() throws IOException {
22992341
}
23002342
}
23012343

2344+
@Controller
2345+
public static class RequestBodyArgMismatchController {
2346+
2347+
@RequestMapping(value = "/something", method = RequestMethod.PUT)
2348+
public void handle(@RequestBody A a) throws IOException {
2349+
}
2350+
}
2351+
2352+
@XmlRootElement
2353+
public static class A {
2354+
2355+
}
2356+
2357+
@XmlRootElement
2358+
public static class B {
2359+
2360+
}
2361+
2362+
23022363
public static class NotReadableMessageConverter implements HttpMessageConverter {
23032364

23042365
public boolean canRead(Class clazz, MediaType mediaType) {

0 commit comments

Comments
 (0)