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);
|
||||
}
|
||||
Reference in New Issue
Block a user