|
48 | 48 | import javax.servlet.http.HttpSession;
|
49 | 49 | import javax.validation.Valid;
|
50 | 50 | import javax.validation.constraints.NotNull;
|
| 51 | +import javax.xml.bind.annotation.XmlRootElement; |
51 | 52 |
|
52 | 53 | import static org.junit.Assert.*;
|
53 | 54 | import org.junit.Test;
|
|
60 | 61 | import org.springframework.beans.GenericBean;
|
61 | 62 | import org.springframework.beans.ITestBean;
|
62 | 63 | import org.springframework.beans.TestBean;
|
| 64 | +import org.springframework.beans.factory.BeanCreationException; |
63 | 65 | import org.springframework.beans.factory.annotation.Autowired;
|
64 | 66 | import org.springframework.beans.factory.annotation.Value;
|
65 | 67 | import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
|
|
81 | 83 | import org.springframework.http.converter.HttpMessageNotReadableException;
|
82 | 84 | import org.springframework.http.converter.HttpMessageNotWritableException;
|
83 | 85 | import org.springframework.http.converter.StringHttpMessageConverter;
|
| 86 | +import org.springframework.http.converter.xml.MarshallingHttpMessageConverter; |
84 | 87 | import org.springframework.mock.web.MockHttpServletRequest;
|
85 | 88 | import org.springframework.mock.web.MockHttpServletResponse;
|
86 | 89 | import org.springframework.mock.web.MockServletConfig;
|
87 | 90 | import org.springframework.mock.web.MockServletContext;
|
| 91 | +import org.springframework.oxm.jaxb.Jaxb2Marshaller; |
88 | 92 | import org.springframework.stereotype.Controller;
|
89 | 93 | import org.springframework.ui.ExtendedModelMap;
|
90 | 94 | import org.springframework.ui.Model;
|
@@ -1256,6 +1260,44 @@ public void responseBodyVoid() throws ServletException, IOException {
|
1256 | 1260 | assertEquals(200, response.getStatus());
|
1257 | 1261 | }
|
1258 | 1262 |
|
| 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 | + |
1259 | 1301 |
|
1260 | 1302 | @Test
|
1261 | 1303 | public void contentTypeHeaders() throws ServletException, IOException {
|
@@ -2299,6 +2341,25 @@ public void handle() throws IOException {
|
2299 | 2341 | }
|
2300 | 2342 | }
|
2301 | 2343 |
|
| 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 | + |
2302 | 2363 | public static class NotReadableMessageConverter implements HttpMessageConverter {
|
2303 | 2364 |
|
2304 | 2365 | public boolean canRead(Class clazz, MediaType mediaType) {
|
|
0 commit comments