Use the type of the actual return value in @MVC
The new @MVC support classes select a HandlerMethodArgumentResolver and a HandlerMethodReturnValueHandler statically, i.e. based on the signature of the method, which means that a controller method can't declare a more general return type like Object but actually return a more specific one, e.g. String vs RedirectView, and expect the right handler to be used. The fix ensures that a HandlerMethodReturnValueHandler is selected based on the actual return value type, which is something that was supported with the old @MVC support classes. One consequence of the change is the selected HandlerMethodReturnValueHandler can no longer be cached but that matches the behavior of the old @MVC support classes. Issues: SPR-9218
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2012 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.
|
||||
@@ -15,9 +15,10 @@
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.mvc.method.annotation;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@@ -30,17 +31,20 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.converter.HttpMessageNotWritableException;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.method.annotation.RequestParamMethodArgumentResolver;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolverComposite;
|
||||
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
|
||||
import org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
import org.springframework.web.servlet.view.RedirectView;
|
||||
|
||||
/**
|
||||
* Test fixture with {@link ServletInvocableHandlerMethod}.
|
||||
*
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class ServletInvocableHandlerMethodTests {
|
||||
@@ -53,6 +57,8 @@ public class ServletInvocableHandlerMethodTests {
|
||||
|
||||
private ServletWebRequest webRequest;
|
||||
|
||||
private MockHttpServletRequest request;
|
||||
|
||||
private MockHttpServletResponse response;
|
||||
|
||||
@Before
|
||||
@@ -60,8 +66,9 @@ public class ServletInvocableHandlerMethodTests {
|
||||
returnValueHandlers = new HandlerMethodReturnValueHandlerComposite();
|
||||
argumentResolvers = new HandlerMethodArgumentResolverComposite();
|
||||
mavContainer = new ModelAndViewContainer();
|
||||
request = new MockHttpServletRequest();
|
||||
response = new MockHttpServletResponse();
|
||||
webRequest = new ServletWebRequest(new MockHttpServletRequest(), response);
|
||||
webRequest = new ServletWebRequest(request, response);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -92,29 +99,45 @@ public class ServletInvocableHandlerMethodTests {
|
||||
webRequest.getNativeRequest(MockHttpServletRequest.class).addHeader("If-Modified-Since", 10 * 1000 * 1000);
|
||||
int lastModifiedTimestamp = 1000 * 1000;
|
||||
webRequest.checkNotModified(lastModifiedTimestamp);
|
||||
|
||||
|
||||
ServletInvocableHandlerMethod handlerMethod = getHandlerMethod("notModified");
|
||||
handlerMethod.invokeAndHandle(webRequest, mavContainer);
|
||||
|
||||
assertTrue("Null return value + 'not modified' request should result in 'request handled'",
|
||||
mavContainer.isRequestHandled());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@Test(expected=HttpMessageNotWritableException.class)
|
||||
public void exceptionWhileHandlingReturnValue() throws Exception {
|
||||
returnValueHandlers.addHandler(new ExceptionRaisingReturnValueHandler());
|
||||
|
||||
ServletInvocableHandlerMethod handlerMethod = getHandlerMethod("handle");
|
||||
try {
|
||||
handlerMethod.invokeAndHandle(webRequest, mavContainer);
|
||||
fail("Expected exception");
|
||||
} catch (HttpMessageNotWritableException ex) {
|
||||
// Expected..
|
||||
// Allow HandlerMethodArgumentResolver exceptions to propagate..
|
||||
}
|
||||
handlerMethod.invokeAndHandle(webRequest, mavContainer);
|
||||
fail("Expected exception");
|
||||
}
|
||||
|
||||
private ServletInvocableHandlerMethod getHandlerMethod(String methodName, Class<?>... argTypes)
|
||||
@Test
|
||||
public void dynamicReturnValue() throws Exception {
|
||||
argumentResolvers.addResolver(new RequestParamMethodArgumentResolver(null, false));
|
||||
returnValueHandlers.addHandler(new ViewMethodReturnValueHandler());
|
||||
returnValueHandlers.addHandler(new ViewNameMethodReturnValueHandler());
|
||||
|
||||
// Invoke without a request parameter (String return value)
|
||||
ServletInvocableHandlerMethod handlerMethod = getHandlerMethod("dynamicReturnValue", String.class);
|
||||
handlerMethod.invokeAndHandle(webRequest, mavContainer);
|
||||
|
||||
assertNotNull(mavContainer.getView());
|
||||
assertEquals(RedirectView.class, mavContainer.getView().getClass());
|
||||
|
||||
// Invoke with a request parameter (RedirectView return value)
|
||||
request.setParameter("param", "value");
|
||||
handlerMethod.invokeAndHandle(webRequest, mavContainer);
|
||||
|
||||
assertEquals("view", mavContainer.getViewName());
|
||||
}
|
||||
|
||||
|
||||
private ServletInvocableHandlerMethod getHandlerMethod(String methodName, Class<?>... argTypes)
|
||||
throws NoSuchMethodException {
|
||||
Method method = Handler.class.getDeclaredMethod(methodName, argTypes);
|
||||
ServletInvocableHandlerMethod handlerMethod = new ServletInvocableHandlerMethod(new Handler(), method);
|
||||
@@ -133,13 +156,16 @@ public class ServletInvocableHandlerMethodTests {
|
||||
@ResponseStatus(value = HttpStatus.BAD_REQUEST, reason = "400 Bad Request")
|
||||
public void responseStatus() {
|
||||
}
|
||||
|
||||
|
||||
public void httpServletResponse(HttpServletResponse response) {
|
||||
}
|
||||
|
||||
|
||||
public void notModified() {
|
||||
}
|
||||
|
||||
|
||||
public Object dynamicReturnValue(@RequestParam(required=false) String param) {
|
||||
return (param != null) ? "view" : new RedirectView("redirectView");
|
||||
}
|
||||
}
|
||||
|
||||
private static class ExceptionRaisingReturnValueHandler implements HandlerMethodReturnValueHandler {
|
||||
|
||||
Reference in New Issue
Block a user