Spring Web : HandlerMethodReturnValueHandler

本文深入探讨Spring Web中HandlerMethodReturnValueHandler接口的作用及其核心方法。该接口负责处理控制器方法的返回值,通过supportsReturnType方法判断支持的返回类型,handleReturnValue方法则负责具体的返回值处理逻辑,包括响应信息的设置及请求的最终处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述

HandlerMethodReturnValueHandlerSpring Web的一个策略接口,该类对象用于处理控制器方法的返回值。该接口定义了两个方法 :

  • boolean supportsReturnType(MethodParameter returnType)

    检测该返回值是否被支持,通常是根据返回值的类型或者注解来判断;只有被支持的返回值类型才能被该HandlerMethodReturnValueHandler的另一个方法handleReturnValue处理。

  • void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType,ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception

    处理返回值;只有被当前HandlerMethodReturnValueHandler的另一方法supportsReturnType判断为支持的类型才能被该方法处理;该方法通常有两种处理结果分支:

    1. 能够将请求处理完,此时会将相应的信息,比如头部,状态字,或者响应体设置到响应对象,然后标记mavContainer的属性requestHandledtrue,告知调用者请求已被完全处理,无需继续处理该请求;
    2. 不能将请求处理完,也有可能将部分信息设置到响应对象,比如头部,状态字等等,不过mavContainer的属性requestHandled会是false,告知调用者请求尚未处理完,需要继续处理,通常该请求下mavContainer隐含了要渲染的视图和需要被渲染的数据模型,调用者随后会进行视图解析和视图渲染才标记成请求处理完成。

Spring内置提供了很多HandlerMethodReturnValueHandler的实现,具体可以参考:Spring Web : HandlerMethodReturnValueHandler 实现类清单

关于HandlerMethodReturnValueHandler的使用,可以参考Spring MVC HandlerAdapter : RequestMappingHandlerAdapter

源代码

package org.springframework.web.method.support;

import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.web.context.request.NativeWebRequest;

public interface HandlerMethodReturnValueHandler {

	/**
	 * Whether the given {@linkplain MethodParameter method return type} is
	 * supported by this handler.
     * 是否支持处理某个返回值 returnType
     * 注意 : 方法返回值 也被 Spring 叫做一个方法参数
	 * @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.
     * 处理控制器方法执行的返回值,将处理结果传播到 mavContainer 和 webRequest,
     * 如果该方法实现逻辑能够将请求处理完,则调用 ModelAndViewContainer#setRequestHandled(true),
     * 否则会将信息传播到 mavContainer 供调用者继续处理
	 * @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(@Nullable Object returnValue, MethodParameter returnType,
			ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception;

}

参考文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值