SpringMVC源码解析之ServletInvocableHandlerMethod

举报
JavaEdge 发表于 2021/06/04 00:20:12 2021/06/04
【摘要】 InvocableHandlerMethod 提供了一种方法,用于调用处理器方法,处理给定的请求,其已通过注册的HandlerMethodArgumentResolver解析了方法参数值。 参数解析往往需要WebDataBinder用于数据结合或进行类型转换。 使用setDataBinderFactory(WebDataBinderFactory)属性来提供一种粘合...

InvocableHandlerMethod

提供了一种方法,用于调用处理器方法,处理给定的请求,其已通过注册的HandlerMethodArgumentResolver解析了方法参数值。

参数解析往往需要WebDataBinder用于数据结合或进行类型转换。 使用setDataBinderFactory(WebDataBinderFactory)属性来提供一种粘合剂厂传递给参数解析器。
使用setHandlerMethodArgumentResolvers自定义的参数解析器的列表。

invokeForRequest

解析给定请求的上下文中其参数值后调用指定方法。
参数值是通过HandlerMethodArgumentResolver解析的。

doInvoke

调用与给定的参数值的处理方法

	protected Object doInvoke(Object... args) throws Exception {
		ReflectionUtils.makeAccessible(getBridgedMethod());
		try { return getBridgedMethod().invoke(getBean(), args);
		}
		catch (IllegalArgumentException ex) { assertTargetBean(getBridgedMethod(), getBean(), args); String text = (ex.getMessage() != null ? ex.getMessage() : "Illegal argument"); throw new IllegalStateException(getInvocationErrorMessage(text, args), ex);
		}
		catch (InvocationTargetException ex) { // Unwrap for HandlerExceptionResolvers ... Throwable targetException = ex.getTargetException(); if (targetException instanceof RuntimeException) { throw (RuntimeException) targetException; } else if (targetException instanceof Error) { throw (Error) targetException; } else if (targetException instanceof Exception) { throw (Exception) targetException; } else { String text = getInvocationErrorMessage("Failed to invoke handler method", args); throw new IllegalStateException(text, targetException); }
		}
	}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

ServletInvocableHandlerMethod

扩展InvocableHandlerMethod通过注册的能力来处理返回值HandlerMethodReturnValueHandler并且还支持设置基于方法级响应状态@ResponseStatus注解。
甲null返回值(包括空隙)可以被解释为请求处理结束结合有@ResponseStatus注释,未改性的检查条件(见ServletWebRequest.checkNotModified(long) ),或提供对所述接入的方法的参数响应流

invokeAndHandle

调用该方法,并通过所配置的HandlerMethodReturnValueHandler处理返回值

  • WebRequest - 当前请求
  • mavContainer - 在ModelAndViewContainer此请求
  • providedArgs - “给”论据类型匹配(未解析)
public void invokeAndHandle(ServletWebRequest webRequest, ModelAndViewContainer mavContainer,
		Object... providedArgs) throws Exception {

	Object returnValue = invokeForRequest(webRequest, mavContainer, providedArgs);
	setResponseStatus(webRequest);

	if (returnValue == null) {
		if (isRequestNotModified(webRequest) || getResponseStatus() != null || mavContainer.isRequestHandled()) { mavContainer.setRequestHandled(true); return;
		}
	}
	else if (StringUtils.hasText(getResponseStatusReason())) {
		mavContainer.setRequestHandled(true);
		return;
	}

	mavContainer.setRequestHandled(false);
	try {
		this.returnValueHandlers.handleReturnValue( returnValue, getReturnValueType(returnValue), mavContainer, webRequest);
	}
	catch (Exception ex) {
		if (logger.isTraceEnabled()) { logger.trace(getReturnValueHandlingErrorMessage("Error handling return value", returnValue), ex);
		}
		throw ex;
	}
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

文章来源: javaedge.blog.csdn.net,作者:JavaEdge.,版权归原作者所有,如需转载,请联系作者。

原文链接:javaedge.blog.csdn.net/article/details/106551309

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。