SpringMVC重要接口(二)HandlerMethodReturnValueHandler
2017-11-02 20:49
447 查看
我们在上一节提到,SpringMVC方法中的参数都是靠
我们都使用过
![](http://img.blog.csdn.net/20170523093014775)
![](http://img.blog.csdn.net/20170522113617289)
package org.springframework.web.method.support;
import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;
/**
* Strategy interface to handle the value returned from the invocation of a
* handler method .
*
* @author Arjen Poutsma
* @since 3.1
* @see HandlerMethodArgumentResolver
*/
public interface HandlerMethodReturnValueHandler {
/**
* Whether the given {@linkplain MethodParameter method return type} is
* supported by this handler.
* @param returnType the method return type to check
* @return {@code true} if this handler supports the supplied return type;
* {@code false} otherwise
*/
boolean supportsReturnType(MethodParameter returnType);
/**
* Handle the given return value by adding attributes to the model and
* setting a view or setting the
* {@link ModelAndViewContainer#setRequestHandled} flag to {@code true}
* to indicate the response has been handled directly.
* @param returnValue the value returned from the handler method
* @param returnType the type of the return value. This type must have
* previously been passed to {@link #supportsReturnType} which must
* have returned {@code true}.
* @param mavContainer the ModelAndViewContainer for the current request
* @param webRequest the current request
* @throws Exception if the return value handling results in an error
*/
void handleReturnValue(Object returnValue, MethodParameter returnType,
ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception;
}
通过该方法我们如果需要对某个参数进行处理 只要此处返回true即可,
通过
@Override
public boolean supportsReturnType(MethodParameter returnType) {
return (AnnotatedElementUtils.hasAnnotation(returnType.getContainingClass(), ResponseBody.class) || returnType.hasMethodAnnotation(ResponseBody.class));
}
@Override
public boolean supportsReturnType(MethodParameter returnType) {
return ModelAndView.class.isAssignableFrom(returnType.getParameterType());
}
实现对返回值的处理操作。
![](http://img.blog.csdn.net/20170522113733244)
![](http://img.blog.csdn.net/20170523095951688)
![](http://img.blog.csdn.net/20170523100228111)
SpringMVC自己的
其实定义在
* Return the list of return value handlers to use including built-in and
* custom handlers provided via {@link #setReturnValueHandlers}.
*/
private List<HandlerMethodReturnValueHandler> getDefaultReturnValueHandlers() {
List<HandlerMethodReturnValueHandler> handlers = new ArrayList<HandlerMethodReturnValueHandler>();
// Single-purpose return value types
handlers.add(new ModelAndViewMethodReturnValueHandler());
handlers.add(new ModelMethodProcessor());
handlers.add(new ViewMethodReturnValueHandler());
handlers.add(new ResponseBodyEmitterReturnValueHandler(getMessageConverters()));
handlers.add(new StreamingResponseBodyReturnValueHandler());
handlers.add(new HttpEntityMethodProcessor(getMessageConverters(),
this.contentNegotiationManager, this.requestResponseBodyAdvice));
handlers.add(new HttpHeadersReturnValueHandler());
handlers.add(new CallableMethodReturnValueHandler());
handlers.add(new DeferredResultMethodReturnValueHandler());
handlers.add(new AsyncTaskMethodReturnValueHandler(this.beanFactory));
// Annotation-based return value types
handlers.add(new ModelAttributeMethodProcessor(false));
handlers.add(new RequestResponseBodyMethodProcessor(getMessageConverters(),
this.contentNegotiationManager, this.requestResponseBodyAdvice));
// Multi-purpose return value types
handlers.add(new ViewNameMethodReturnValueHandler());
handlers.add(new MapMethodProcessor());
// Custom return value types
if (getCustomReturnValueHandlers() != null) {
handlers.addAll(getCustomReturnValueHandlers());
}
// Catch-all
if (!CollectionUtils.isEmpty(getModelAndViewResolvers())) {
handlers.add(new ModelAndViewResolverMethodReturnValueHandler(getModelAndViewResolvers()));
}
else {
handlers.add(new ModelAttributeMethodProcessor(true));
}
return handlers;
}
![](http://img.blog.csdn.net/20170523103010498)
<mvc:annotation-driven>
<mvc:return-value-handlers>
<bean class=""/>
</mvc:return-value-handlers>
</mvc:annotation-driven>
HandlerMethodArgumentResolver解析的,那返回值呢。
我们都使用过
@ResponseBody注解,也知道它的作用,那又是谁来解析这个注解,使之输出我们想要的东西,本节我们一起来剖析
org.springframework.web.method.support.HandlerMethodReturnValueHandler。
package org.springframework.web.method.support;
import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;
/**
* Strategy interface to handle the value returned from the invocation of a
* handler method .
*
* @author Arjen Poutsma
* @since 3.1
* @see HandlerMethodArgumentResolver
*/
public interface HandlerMethodReturnValueHandler {
/**
* Whether the given {@linkplain MethodParameter method return type} is
* supported by this handler.
* @param returnType the method return type to check
* @return {@code true} if this handler supports the supplied return type;
* {@code false} otherwise
*/
boolean supportsReturnType(MethodParameter returnType);
/**
* Handle the given return value by adding attributes to the model and
* setting a view or setting the
* {@link ModelAndViewContainer#setRequestHandled} flag to {@code true}
* to indicate the response has been handled directly.
* @param returnValue the value returned from the handler method
* @param returnType the type of the return value. This type must have
* previously been passed to {@link #supportsReturnType} which must
* have returned {@code true}.
* @param mavContainer the ModelAndViewContainer for the current request
* @param webRequest the current request
* @throws Exception if the return value handling results in an error
*/
void handleReturnValue(Object returnValue, MethodParameter returnType,
ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception;
}
HandlerMethodArgumentResolver接口同样包含两个接口方法
supportsReturnType
通过该方法我们如果需要对某个参数进行处理 只要此处返回true即可,
通过
MethodParameter可以获取该方法返回值上的一些信息, 如方法返回值中的注解信息等
@Override
public boolean supportsReturnType(MethodParameter returnType) {
return (AnnotatedElementUtils.hasAnnotation(returnType.getContainingClass(), ResponseBody.class) || returnType.hasMethodAnnotation(ResponseBody.class));
}
@Override
public boolean supportsReturnType(MethodParameter returnType) {
return ModelAndView.class.isAssignableFrom(returnType.getParameterType());
}
resolveArgument
实现对返回值的处理操作。
SpringMVC自己的
HandlerMethodReturnValueHandler有哪些,并且会以什么样的顺序执行呢?
其实定义在
RequestMappingHandlerAdapter里:/**
* Return the list of return value handlers to use including built-in and
* custom handlers provided via {@link #setReturnValueHandlers}.
*/
private List<HandlerMethodReturnValueHandler> getDefaultReturnValueHandlers() {
List<HandlerMethodReturnValueHandler> handlers = new ArrayList<HandlerMethodReturnValueHandler>();
// Single-purpose return value types
handlers.add(new ModelAndViewMethodReturnValueHandler());
handlers.add(new ModelMethodProcessor());
handlers.add(new ViewMethodReturnValueHandler());
handlers.add(new ResponseBodyEmitterReturnValueHandler(getMessageConverters()));
handlers.add(new StreamingResponseBodyReturnValueHandler());
handlers.add(new HttpEntityMethodProcessor(getMessageConverters(),
this.contentNegotiationManager, this.requestResponseBodyAdvice));
handlers.add(new HttpHeadersReturnValueHandler());
handlers.add(new CallableMethodReturnValueHandler());
handlers.add(new DeferredResultMethodReturnValueHandler());
handlers.add(new AsyncTaskMethodReturnValueHandler(this.beanFactory));
// Annotation-based return value types
handlers.add(new ModelAttributeMethodProcessor(false));
handlers.add(new RequestResponseBodyMethodProcessor(getMessageConverters(),
this.contentNegotiationManager, this.requestResponseBodyAdvice));
// Multi-purpose return value types
handlers.add(new ViewNameMethodReturnValueHandler());
handlers.add(new MapMethodProcessor());
// Custom return value types
if (getCustomReturnValueHandlers() != null) {
handlers.addAll(getCustomReturnValueHandlers());
}
// Catch-all
if (!CollectionUtils.isEmpty(getModelAndViewResolvers())) {
handlers.add(new ModelAndViewResolverMethodReturnValueHandler(getModelAndViewResolvers()));
}
else {
handlers.add(new ModelAttributeMethodProcessor(true));
}
return handlers;
}
HandlerMethodReturnValueHandler子类 | 注解 or 类 |
---|---|
org.springframework.web.servlet.mvc.method.annotation.ModelAndViewMethodReturnValueHandler | ModelAndView |
org.springframework.web.method.annotation.ModelMethodProcessor | Model |
org.springframework.web.servlet.mvc.method.annotation.ViewMethodReturnValueHandler | View |
org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitterReturnValueHandler | ResponseEntity |
org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBodyReturnValueHandler | StreamingResponseBody |
org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor | HttpEntity |
org.springframework.web.servlet.mvc.method.annotation.HttpHeadersReturnValueHandler | HttpHeaders |
org.springframework.web.servlet.mvc.method.annotation.CallableMethodReturnValueHandler | Callable |
org.springframework.web.servlet.mvc.method.annotation.DeferredResultMethodReturnValueHandler | |
org.springframework.web.servlet.mvc.method.annotation.AsyncTaskMethodReturnValueHandler | WebAsyncTask |
org.springframework.web.method.annotation.ModelAttributeMethodProcessor | @ModelAttribute |
org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor | @ResponseBody |
org.springframework.web.servlet.mvc.method.annotation.ViewNameMethodReturnValueHandler | CharSequence 、void |
org.springframework.web.method.annotation.MapMethodProcessor | Map |
<mvc:annotation-driven>
<mvc:return-value-handlers>
<bean class=""/>
</mvc:return-value-handlers>
</mvc:annotation-driven>
相关文章推荐
- SpringMVC重要接口(二)HandlerMethodReturnValueHandler
- 2.盘点springmvc的常用接口之HandlerMethodReturnValueHandler
- SpringMVC之分析HandlerMethodReturnValueHandler(二)
- springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)
- springMVC源码分析--HandlerMethodReturnValueHandlerComposite返回值解析器集合(二)
- SpringMVC源码总结(六)mvc:annotation-driven中的HandlerMethodReturnValueHandler
- 【原创】遨游springmvc之HandlerMethodReturnValueHandler
- SpringMVC之分析HandlerMethodReturnValueHandler(一)
- springMVC源码分析--HandlerMethodReturnValueHandlerComposite返回值解析器集合(二)
- springmvc核心之HandlerMethodReturnValueHandler
- springMVC源码分析--HandlerMethodReturnValueHandlerComposite返回值解析器集合(二)
- Springmvc的HandlerMethodReturnValueHandle
- springMVC源码分析--ViewNameMethodReturnValueHandler返回值处理器(三)
- springmvc核心之HandlerMethodReturnValueHandler
- SpringMVC重要接口(一)HandlerMethodArgumentResolver
- SpringMVC源码总结(六)mvc:annotation-driven中的HandlerMethodReturnValueHandler