SPR-5426 - Allow for custom processing or result objects returned from handler/controller methods
This commit is contained in:
@@ -150,6 +150,8 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
|
||||
|
||||
private WebArgumentResolver[] customArgumentResolvers;
|
||||
|
||||
private ModelAndViewResolver[] customModelAndViewResolvers;
|
||||
|
||||
private final Map<Class<?>, ServletHandlerMethodResolver> methodResolverCache =
|
||||
new ConcurrentHashMap<Class<?>, ServletHandlerMethodResolver>();
|
||||
|
||||
@@ -269,7 +271,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a custom ArgumentResolvers to use for special method parameter types. Such a custom ArgumentResolver will kick
|
||||
* Set a custom WebArgumentResolvers to use for special method parameter types. Such a custom WebArgumentResolver will kick
|
||||
* in first, having a chance to resolve an argument value before the standard argument handling kicks in.
|
||||
*/
|
||||
public void setCustomArgumentResolver(WebArgumentResolver argumentResolver) {
|
||||
@@ -277,13 +279,29 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
|
||||
}
|
||||
|
||||
/**
|
||||
* Set one or more custom ArgumentResolvers to use for special method parameter types. Any such custom ArgumentResolver
|
||||
* Set one or more custom WebArgumentResolvers to use for special method parameter types. Any such custom WebArgumentResolver
|
||||
* will kick in first, having a chance to resolve an argument value before the standard argument handling kicks in.
|
||||
*/
|
||||
public void setCustomArgumentResolvers(WebArgumentResolver[] argumentResolvers) {
|
||||
this.customArgumentResolvers = argumentResolvers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a custom ModelAndViewResolvers to use for special method return types. Such a custom ModelAndViewResolver will kick
|
||||
* in first, having a chance to resolve an return value before the standard ModelAndView handling kicks in.
|
||||
*/
|
||||
public void setCustomModelAndViewResolver(ModelAndViewResolver customModelAndViewResolver) {
|
||||
this.customModelAndViewResolvers = new ModelAndViewResolver[]{customModelAndViewResolver};
|
||||
}
|
||||
|
||||
/**
|
||||
* Set one or more custom ModelAndViewResolvers to use for special method return types. Any such custom ModelAndViewResolver
|
||||
* will kick in first, having a chance to resolve an return value before the standard ModelAndView handling kicks in.
|
||||
*/
|
||||
public void setCustomModelAndViewResolvers(ModelAndViewResolver[] customModelAndViewResolvers) {
|
||||
this.customModelAndViewResolvers = customModelAndViewResolvers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the message body converters to use. These converters are used to convert from and to HTTP requests and
|
||||
* responses.
|
||||
@@ -673,6 +691,16 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
|
||||
ExtendedModelMap implicitModel,
|
||||
ServletWebRequest webRequest) {
|
||||
|
||||
// Invoke custom resolvers if present...
|
||||
if (customModelAndViewResolvers != null) {
|
||||
for (ModelAndViewResolver mavResolver : customModelAndViewResolvers) {
|
||||
ModelAndView mav = mavResolver
|
||||
.resolveModelAndView(handlerMethod, handlerType, returnValue, implicitModel, webRequest);
|
||||
if (mav != ModelAndViewResolver.UNRESOLVED) {
|
||||
return mav;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (handlerMethod.isAnnotationPresent(ResponseStatus.class)) {
|
||||
ResponseStatus responseStatus = handlerMethod.getAnnotation(ResponseStatus.class);
|
||||
HttpServletResponse response = webRequest.getResponse();
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.mvc.annotation;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.ui.ExtendedModelMap;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
/**
|
||||
* SPI for resolving custom return values from a specific handler method. Typically implemented to detect special return
|
||||
* types, resolving well-known result values for them.
|
||||
*
|
||||
* <p>A typical implementation could look like as follows:
|
||||
*
|
||||
* <pre class="code">
|
||||
* public class MyModelAndViewResolver implements ModelAndViewResolver {
|
||||
*
|
||||
* public ModelAndView resolveModelAndView(Method handlerMethod,
|
||||
* Class handlerType,
|
||||
* Object returnValue,
|
||||
* ExtendedModelMap implicitModel,
|
||||
* NativeWebRequest webRequest) {
|
||||
* if (returnValue instanceof MySpecialRetVal.class)) {
|
||||
* return new MySpecialRetVal(returnValue);
|
||||
* }
|
||||
* return UNRESOLVED;
|
||||
* }
|
||||
* }</pre>
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#setCustomModelAndViewResolvers
|
||||
* @see org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter#setCustomModelAndViewResolvers
|
||||
* @since 3.0
|
||||
*/
|
||||
public interface ModelAndViewResolver {
|
||||
|
||||
/** Marker to be returned when the resolver does not know how to handle the given method parameter. */
|
||||
ModelAndView UNRESOLVED = new ModelAndView();
|
||||
|
||||
ModelAndView resolveModelAndView(Method handlerMethod,
|
||||
Class handlerType,
|
||||
Object returnValue,
|
||||
ExtendedModelMap implicitModel,
|
||||
NativeWebRequest webRequest);
|
||||
}
|
||||
@@ -18,6 +18,7 @@ package org.springframework.web.servlet.mvc.annotation;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.Method;
|
||||
import java.security.Principal;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
@@ -924,6 +925,32 @@ public class ServletAnnotationControllerTests {
|
||||
assertEquals(201, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mavResolver() throws ServletException, IOException {
|
||||
@SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() {
|
||||
@Override
|
||||
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
|
||||
GenericWebApplicationContext wac = new GenericWebApplicationContext();
|
||||
wac.registerBeanDefinition("controller",
|
||||
new RootBeanDefinition(ModelAndViewResolverController.class));
|
||||
RootBeanDefinition adapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class);
|
||||
adapterDef.getPropertyValues()
|
||||
.addPropertyValue("customModelAndViewResolver", new MyModelAndViewResolver());
|
||||
wac.registerBeanDefinition("handlerAdapter", adapterDef);
|
||||
wac.refresh();
|
||||
return wac;
|
||||
}
|
||||
};
|
||||
servlet.init(new MockServletConfig());
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
servlet.service(request, response);
|
||||
assertEquals("myValue", response.getContentAsString());
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Controllers
|
||||
*/
|
||||
@@ -1502,6 +1529,27 @@ public class ServletAnnotationControllerTests {
|
||||
}
|
||||
}
|
||||
|
||||
public static class MyMessageConverter implements HttpMessageConverter {
|
||||
|
||||
public boolean supports(Class clazz) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public List getSupportedMediaTypes() {
|
||||
return Collections.singletonList(new MediaType("application", "pdf"));
|
||||
}
|
||||
|
||||
public Object read(Class clazz, HttpInputMessage inputMessage)
|
||||
throws IOException, HttpMessageNotReadableException {
|
||||
throw new HttpMessageNotReadableException("Could not read");
|
||||
}
|
||||
|
||||
public void write(Object o, HttpOutputMessage outputMessage)
|
||||
throws IOException, HttpMessageNotWritableException {
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
}
|
||||
}
|
||||
|
||||
@Controller
|
||||
public static class HeadersController {
|
||||
|
||||
@@ -1526,25 +1574,40 @@ public class ServletAnnotationControllerTests {
|
||||
}
|
||||
}
|
||||
|
||||
public static class MyMessageConverter implements HttpMessageConverter {
|
||||
|
||||
public boolean supports(Class clazz) {
|
||||
return true;
|
||||
}
|
||||
@Controller
|
||||
public static class ModelAndViewResolverController {
|
||||
|
||||
public List getSupportedMediaTypes() {
|
||||
return Collections.singletonList(new MediaType("application", "pdf"));
|
||||
}
|
||||
|
||||
public Object read(Class clazz, HttpInputMessage inputMessage)
|
||||
throws IOException, HttpMessageNotReadableException {
|
||||
throw new HttpMessageNotReadableException("Could not read");
|
||||
}
|
||||
|
||||
public void write(Object o, HttpOutputMessage outputMessage)
|
||||
throws IOException, HttpMessageNotWritableException {
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
@RequestMapping("/")
|
||||
public MySpecialArg handle() {
|
||||
return new MySpecialArg("foo");
|
||||
}
|
||||
}
|
||||
|
||||
public static class MyModelAndViewResolver implements ModelAndViewResolver {
|
||||
|
||||
public ModelAndView resolveModelAndView(Method handlerMethod,
|
||||
Class handlerType,
|
||||
Object returnValue,
|
||||
ExtendedModelMap implicitModel,
|
||||
NativeWebRequest webRequest) {
|
||||
if (returnValue instanceof MySpecialArg) {
|
||||
return new ModelAndView(new View() {
|
||||
public String getContentType() {
|
||||
return "text/html";
|
||||
}
|
||||
|
||||
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
|
||||
throws Exception {
|
||||
response.getWriter().write("myValue");
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
return UNRESOLVED;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user