SpringMVC重要接口(二)HandlerMethodReturnValueHandler
来源:互联网 发布:g76车锥螺纹编程实例 编辑:程序博客网 时间:2024/06/16 12:21
我们在上一节提到,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.ModelAndViewMethodReturnValueHandlerModelAndVieworg.springframework.web.method.annotation.ModelMethodProcessorModelorg.springframework.web.servlet.mvc.method.annotation.ViewMethodReturnValueHandlerVieworg.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitterReturnValueHandlerResponseEntityorg.springframework.web.servlet.mvc.method.annotation.StreamingResponseBodyReturnValueHandlerStreamingResponseBodyorg.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessorHttpEntityorg.springframework.web.servlet.mvc.method.annotation.HttpHeadersReturnValueHandlerHttpHeadersorg.springframework.web.servlet.mvc.method.annotation.CallableMethodReturnValueHandlerCallableorg.springframework.web.servlet.mvc.method.annotation.DeferredResultMethodReturnValueHandler org.springframework.web.servlet.mvc.method.annotation.AsyncTaskMethodReturnValueHandlerWebAsyncTaskorg.springframework.web.method.annotation.ModelAttributeMethodProcessor@ModelAttributeorg.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor@ResponseBodyorg.springframework.web.servlet.mvc.method.annotation.ViewNameMethodReturnValueHandlerCharSequence 、voidorg.springframework.web.method.annotation.MapMethodProcessorMap
- <mvc:annotation-driven>
- <mvc:return-value-handlers>
- <bean class=""/>
- </mvc:return-value-handlers>
- </mvc:annotation-driven>