您的位置:首页 > 编程语言 > Java开发

SpringMVC重要接口(二)HandlerMethodReturnValueHandler

2017-11-02 20:49 447 查看
我们在上一节提到,SpringMVC方法中的参数都是靠
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.ModelAndViewMethodReturnValueHandlerModelAndView
org.springframework.web.method.annotation.ModelMethodProcessorModel
org.springframework.web.servlet.mvc.method.annotation.ViewMethodReturnValueHandlerView
org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitterReturnValueHandlerResponseEntity
org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBodyReturnValueHandlerStreamingResponseBody
org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessorHttpEntity
org.springframework.web.servlet.mvc.method.annotation.HttpHeadersReturnValueHandlerHttpHeaders
org.springframework.web.servlet.mvc.method.annotation.CallableMethodReturnValueHandlerCallable
org.springframework.web.servlet.mvc.method.annotation.DeferredResultMethodReturnValueHandler 
org.springframework.web.servlet.mvc.method.annotation.AsyncTaskMethodReturnValueHandlerWebAsyncTask
org.springframework.web.method.annotation.ModelAttributeMethodProcessor@ModelAttribute
org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor@ResponseBody
org.springframework.web.servlet.mvc.method.annotation.ViewNameMethodReturnValueHandlerCharSequence 、void
org.springframework.web.method.annotation.MapMethodProcessorMap


<mvc:annotation-driven>
<mvc:return-value-handlers>
<bean class=""/>
</mvc:return-value-handlers>
</mvc:annotation-driven>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: